Using ( switch ), write a program that asks the user to enter an integer of type (int) . The number represents the Section at the Faculty of Computer Science, the program prints the name of the section for the following options:
1- If the section number is "1"
"computer science" is printed
2- If the section number is "2"
"Information Technology" is printed
3- If the section number is "3"
"Networks" is printed
4- If the section number is "4"
"Software Engineering" is printed
In the case to enter any number other than the numbers shown above are printed
"error number , try again"
import java.util.Scanner;
public class Solution {
private static boolean state = true;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
while (state) {
System.out.print("> ");
switch (scanner.nextInt()) {
case 1 -> message("Computer Science");
case 2 -> message("Information Technology");
case 3 -> message("Networks");
case 4 -> message("Software Engineering");
default -> message("Error number, try again!");
}
}
}
private static void message(String message) {
state = false;
System.out.println(message);
}
}
Comments
Leave a comment