Is there any way that if a user enters any letter except T, M, or S that it will output other? Or do I have to make a case for every letter and put other? Is there any way to group them for the 'case.' Like for instance,
case "A - Q":
System.out.print("Other");
break;
Obviously you cant do that but something along those lines?
import java.util.Scanner;
public class SwitchPractice
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String employeeCode;
System.out.print("Enter employees code: ");
employeeCode = input.next();
switch (employeeCode)
{
case "T":
System.out.print("Technician");
break;
case "S":
System.out.print("Sales");
break;
case "M":
System.out.print("Marketing");
break;
case "?":
System.out.print("OTHER!");
} } }
Use a default case. For example:
import java.util.Scanner;
public class SwitchPractice
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String employeeCode;
System.out.print("Enter employees code: ");
employeeCode = input.next();
switch (employeeCode)
{
case "T":
System.out.print("Technician");
break;
case "S":
System.out.print("Sales");
break;
case "M":
System.out.print("Marketing");
break;
default:
System.out.print("OTHER!");
break;
}
}
}
This default case will execute whenever the letter is not "T", "M", or "S".