1

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[H\u001b[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!

4

2 回答 2

2

jdk 7 支持在 switch-case 语句中使用字符串。请参阅此链接以获取示例: http
://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html 希望这个有帮助。

于 2012-11-05T03:59:07.530 回答
0

Switch 语句是最干净的方法之一,尽管我不会争论它是否是“最好的”,因为它是主观的。但我来自 C/C++ 背景,对于其他在编写 Java 之前主要编写 C 和 C++ 的人来说,该switch语句是处理多个案例的自然方式。它只是更具可读性。在 C 领域的某一时刻,假设条件测试器可能是最快的条件测试器,因为它使用基于 O(1) 的算法来查找匹配情况,或者匹配默认情况(如果存在)。

但我不太确定它是否适用于 JVM/javac,因为我从未专门对它进行过性能测试。但总而言之,它在语法上是最自然和可读的。

于 2012-11-05T02:51:40.960 回答