In my previous post, I have already made an example similar to this but the difference is that this time we will be using String instead of int. See my sample program below.
Here's how it works...
If you press f it displays Freshman. If you press s it displays Sophomore. If you press j it displays Junior. If you press x it displays Senior. if you press a character other than f,s,j and x, it displays Error.
import java.util.Scanner;
public class YearLvl {
public static void main(String[] args) {
String lvl;
char y;
Scanner reader = new Scanner(System.in);
System.out.println("Enter your year level: ");
lvl = reader.next();
y = lvl.toLowerCase().charAt(0);
switch (y)
{
case 'f': System.out.println("Freshman"); break;
case 's': System.out.println("Sophomore"); break;
case 'j': System.out.println("Junior"); break;
case 'x': System.out.println("Senior"); break;
default: System.out.println("Error");
}
}
}
Here's the output:
Here's how it works...
If you press f it displays Freshman. If you press s it displays Sophomore. If you press j it displays Junior. If you press x it displays Senior. if you press a character other than f,s,j and x, it displays Error.
import java.util.Scanner;
public class YearLvl {
public static void main(String[] args) {
String lvl;
char y;
Scanner reader = new Scanner(System.in);
System.out.println("Enter your year level: ");
lvl = reader.next();
y = lvl.toLowerCase().charAt(0);
switch (y)
{
case 'f': System.out.println("Freshman"); break;
case 's': System.out.println("Sophomore"); break;
case 'j': System.out.println("Junior"); break;
case 'x': System.out.println("Senior"); break;
default: System.out.println("Error");
}
}
}
Here's the output:
Comments
Post a Comment