The switch statement branches the part to be displayed according to the read numerical value.
filename.java
import jva.util.Scanner;
class Abc {
publlic static void(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.print("integer: ");
int n = stdIn.nextInt();
switch (n) {
case 0 : System.out.print("abc");
break;
case 1 : System.out.print("def");
break;
case 2 : System.out.print("ghi");
break;
case 3 :
case 4 : System.out.print("jkl");
break;
default : System.out.print("123");
break;
}
System.out.println();
}
}
In the case of the example, the displayed sentence is divided according to the value of "int n". Also, if the value of n is 3 or 4, the same character is displayed. "Default" is displayed when n is not the value specified for case.
The switch statement ends when the "break statement" at the end of each case is executed. The statement after the switch statement is executed after it is terminated by the break statement.
Recommended Posts