1, enter a ranking, 1th to 4th place, respectively known as the champion, runner-up, third runner-up, third runners, 5 and 5 or more, known as other places.
Import Java.util.scanner;public class switch1{public static void Main (string[] args) { Scanner s=new Scanner (system.in); System.out.println ("Please enter the position you obtained (write Arabic numerals):"); int Mingci=s.nextint (); Switch (MINGCI) {case 1:system.out.println ("Congratulations on winning the championship! "); break; Case 2:system.out.println ("Congratulations on getting runner-up!") "); break; Case 3:system.out.println ("Congratulations on getting the third place!") "); break; Case 4:system.out.println ("Congratulations on your getting third runners!") "); break; Default:System.out.println ("Congratulations on winning the other rankings!") "); break;}}
2, enter a ranking, first to fourth place, respectively known as the champion, runner-up, third runner-up, third runners, five and five or more, known as other places. Test whether you can enter two words, such as "12".
Import Java.util.scanner;public class switch2{public static void Main (string[] args) { Scanner s=new Scanner (system.in); System.out.println ("Please enter the rank you obtained (write one, two or three, four or five, etc.)"); String Mingci=s.next (); Switch (MINGCI) {case "one": System.out.println ("Congratulations on winning the championship!") "); break; Case "Two": System.out.println ("Congratulations on your getting runner-up!") "); break; Case "three": System.out.println ("Congratulations on your third prize!") "); break; Case "Four": System.out.println ("Congratulations on your getting third runners!") "); break; Case "12": System.out.println ("Congratulations on your 12th place!") "); break; Default:System.out.println ("Congratulations on winning the other rankings!") "); break;}}
After testing, you can enter two characters.
Java:switch Statement Example