案例后Java切换/案例将进入默认状态

时间:2014-10-15 04:56:27

标签: java switch-statement case

我正在尝试上课时的作业,而且我使用开关盒来制作命令行界面,但是我对部分I'使用。当我执行addCard命令时,它会很好,但之后会转到默认部分。当我尝试使用任何其他部分,或在没有尝试段的情况下使用它时,它可以工作,并且不会进入默认状态。有关如何修复此问题的任何想法?代码

public static void cmdLine(String Cmd) {
    switch(Cmd) {

        case "Help":
        case "?":
            System.out.println("Available Commands:\naddCard = Add a card!\ndeleteCard = Delete a Card!\nfindCard = Locate Card Number by Name!\nCard (Card Number) = Work with your card");
            break;
        case "addCard" :
            System.out.println ("Enter Account Name:");
            String cName = scan.nextLine();
            try {
                System.out.println ("Enter Account Start Balance:");
                int cBal = scan.nextInt();
                System.out.println ("Enter Account Number:");
                int cNum = scan.nextInt();
                PPArray.addCard(cName, cBal,cNum);
            } catch (InputMismatchException nfe) {
                System.out.println("InputMismatchException: Please try again with only the card number. (Ex. 12345");
                break;
            }
            break;
        case "deleteCard" :
            System.out.println("here we will have a command to remove the card from the array");
            break;
        case "Card" :
            System.out.println("This will lead to a new function to operate with said card");
            break;
        case "Exit":
        case "exit":
        case "Quit":
        case "quit":
            return;
        default :
            System.out.println("Invalid Command ('?' or 'Help' for commands)");
            System.out.println(PPArray.cardArray[1].name);
    }
    cmdLine(scan.nextLine());
}

3 个答案:

答案 0 :(得分:2)

当您在" addCard"中运行try块时,您将获得int cNum = scan.nextInt();的输入。 然后,当你在switch语句之后调用cmdLine(scan.nextLine());时,它会使用新行,这会导致调用default子句。

您可以通过在该try块的末尾添加scan.nextLine()来阻止这种情况。

    case "addCard" :
        System.out.println ("Enter Account Name:");
        String cName = scan.nextLine();
        try {
            System.out.println ("Enter Account Start Balance:");
            int cBal = scan.nextInt();
            System.out.println ("Enter Account Number:");
            int cNum = scan.nextInt();
            PPArray.addCard(cName, cBal,cNum);
            scan.nextLine();
        } catch (InputMismatchException nfe) {
            System.out.println("InputMismatchException: Please try again with only the card number. (Ex. 12345");
            break;
        }
        break;

顺便说一句,我认为递归调用cmdLine(scan.nextLine());以处理下一个输入是错误的编码。我认为一个while循环会更有意义。

答案 1 :(得分:0)

如果要退出程序,则必须实际退出程序:

    case "Exit":
    case "exit":
    case "Quit":
    case "quit":
        return;

应该是

    case "Exit":
    case "exit":
    case "Quit":
    case "quit":
        System.exit(0);

下一行是什么?

cmdLine(scan.nextLine());

正在阅读与任何情况都不匹配的内容,您的步调试器会向您显示逻辑错误的位置。步调试比System.out.println()和猜测更有价值。

答案 2 :(得分:-1)

更改此

case "Exit":
case "exit":
case "Quit":
case "quit":
     return;

case "Exit":
case "exit":
case "Quit":
case "quit":
      break;

修改

另一个小问题是cmdLine(scan.nextLine());每次占用新行并导致default执行。最好使用不接受多个单词或行的cmdLine(scan.next());