*4.18 (Student major and status) Write a program that prompts the user to enter two
characters and displays the major and status represented in the characters. The first
character indicates the major and the second is number character 1, 2, 3, 4, which
indicates whether a student is a freshman, sophomore, junior, or senior. Suppose
the following characters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology
Here is a sample run:
Enter two characters: M1
Mathematics Freshman
Enter two characters: C3
Computer Science Junior
Enter two characters: T3
Invalid input
M: Mathematics
C: Computer Science
I: Information Technology
Here is a sample run:
Enter two characters: M1
Mathematics Freshman
Enter two characters: C3
Computer Science Junior
Enter two characters: T3
Invalid input
import java.util.Scanner; public class ProgrammingEx4_18 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two characters:"); String in = input.nextLine(); char major = in.charAt(0); char level = in.charAt(1); String sMajor = ""; String sLevel = ""; switch (major) { case 'M': sMajor = "Mathematics"; break; case 'C': sMajor = "Computer Science"; break; case 'I': sMajor = "Information Technology"; break; default: System.out.println("Invalid input"); System.exit(0); break; } switch (level) { case '1': sLevel = "freshman"; break; case '2': sLevel = "sophomore"; break; case '3': sLevel = "junior"; break; case '4': sLevel = "senior"; break; default: System.out.println("Invalid input"); System.exit(0); break; } System.out.println(sMajor + " " + sLevel); } }
No comments:
Post a Comment