Answer to Question #243975 in Java | JSP | JSF for Sara

Question #243975

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"


1
Expert's answer
2021-09-29T04:11:45-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS