Using String with Switch statements?
Possible Duplicate:
Switch Statement with Strings in Java
Ok, basically i want to know if this is possible and how to do it. I have a menu and i want this switch statement to recognize chars or a string that was entered by the user.
I have a little menu program that I am writing:
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
public class Personnel
{
// Instance Variables
private static String empName;
private static double wage;
public static void main(String[] args) throws Exception
{
clearScreen();
question();
printMenu();
exit();
}
public static void clearScreen()
{
System.out.println("u001b[Hu001b[2J");
}
private static void exit()
{
System.exit(0);
}
private static void printMenu()
{
System.out.println("t------------------------------------");
System.out.println("t|Commands: n - New employee |");
System.out.println("t| c - Compute paychecks |");
System.out.println("t| r - Raise wages |");
System.out.println("t| p - Print records |");
System.out.println("t| d - Download data |");
System.out.println("t| u - Upload data |");
System.out.println("t| q - Quit |");
System.out.println("t------------------------------------");
System.out.println("");
}
private static void question()
{
System.out.println("Enter command: ");
Scanner q = new Scanner(System.in);
// String input = q.nextLine();
switch (q.nextLine())
{
case "n":
System.out.println("Enter the name of new employee: ");
Scanner stdin = new Scanner(System.in);
System.out.println("Hourly (h) or salaried (s): ");
Scanner stdin2 = new Scanner(System.in);
if (stdin2.equals("h"))
{
System.out.println("Enter hourly wage: ");
Scanner stdin3 = new Scanner(System.in);
}
else
{
System.out.println("Enter annual salary: ");
Scanner stdin4 = new Scanner(System.in);
}
break;
/*
* case 9:
* System.out.println ("Please proceed.");
* new InputMenu();
* break;
* default:
* System.err.println ("Unrecognized option" ); break;
*/
}
}
}
What i am trying to do is make this menu work so that you can type in a command then i will write methods to run depending on what command was typed in. Is switch statement the best way to do this? After they enter in the info i need it to "break" back to the main menu. And anytime during the entire program, if they enter q then the program exits. I'm not sure the best way to go about doing this.
Thanks!
Using String in switch-case statements is supported in jdk 7. Please refer this link for an example: http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html
Hope this helps.
Switch statement is one of the cleanest way to do this, although I wouldn't argue if it were the "best" because its subjective. But me coming from a C/C++ background, and for others too who have mostly written C and C++ before they wrote Java, the switch
statement is a natural way of getting through multiple cases. Its just more readable. And at one point in C land, it was suppose the be the fastest possible condition tester because it used O(1) based algorithm to find the matching case, or matched the default if one existed.
But I am not too sure if its the case on JVM/javac, as I never performance-tested it specifically. But all in all, its the most natural and readable syntactically.
链接地址: http://www.djcxy.com/p/84514.html上一篇: 切换语句和字符串
下一篇: 在Switch语句中使用String?