Programming Reference/Librarys
Question & Answer
Q&A is closed
This is an old revision of the document!
The switch statement is a multi-select. An expression is evaluated once and compared to constants. In case of equality, the instructions that come after the constant processed. In switch statements can only ordinal data types (ie, int, long, int, char, etc.) use.
switch(expression) { case constant1: statements break; case constant2: statements break; case constantn: statements break; default: statements }
package schoolnotes; import java.util.Scanner; public class Schoolnotes{ public static void main(String[] args) { int note; Scanner scan = new Scanner(System.in); System.out.print("Please enter a note: "); note = scan.nextInt(); switch(note) { case 1: System.out.println("very well"); break; case 2: System.out.println("good"); break; case 3: System.out.println("satisfactory"); break; case 4: System.out.println("sufficient"); break; case 5: System.out.println("deficient"); break; case 6: System.out.println("insufficient"); break; default: System.out.println("Not a valid note"); } } }
Please enter a note: 2 good