繁体   English   中英

Java中的开关正在循环

[英]Switch in Java is looping

我的switch语句一直在循环。 它应该打印您选择的选项,然后重新打印菜单。 请帮忙! 这是我的代码:

menu ="\n\t1  Create Account" + 
            "\n\t2  Check balance" +
            "\n\t3  Withdraw" +
            "\n\t1  Deposit" + 
            "\n\t2  Get account ID" +
            "\n\t3  Set ID" +
            "\n\t1  Display Account Info" +

            "\n\t0  Quit\n\n\n";

    System.out.println(menu);
    System.out.println("\tEnter your selection:  ");
    option = scan.nextInt();

while (option != 0) {

            switch (option) {

            case 1: //  Enter and Validate SSN
                        System.out.print("option 1");
                        break;

            case 2:     //Enter and Validate Passwords
                        System.out.print("option 2");
                        break;

            case 3:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 3");
                        break;

            case 4: //  Enter and Validate SSN
                        System.out.print("option 4");
                        break;

            case 5:     //Enter and Validate Passwords
                        System.out.print("option 5");
                        break;

            case 6:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 6");
                        break;

            case 7:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 7");
                        break;

            default: outputString = "\nInvalid Selection\n";
                        System.out.println(outputString);
                        break;

        }
    }

我很确定是您的while循环正在执行循环。 而且,您永远都不会在循环体内更改option的值,因此它当然会连续运行。

大概您想移动这行:

option = scan.nextInt();

到循环的第一行:

while (option != 0) {
    option = scan.nextInt();
    ... 
}

选项永远不会在while循环中更改-如果键入不为0 ,则会导致无限循环

do while循环所需的代码更少:

menu ="\n\t1  Create Account" + 
        "\n\t2  Check balance" +
        "\n\t3  Withdraw" +
        "\n\t1  Deposit" + 
        "\n\t2  Get account ID" +
        "\n\t3  Set ID" +
        "\n\t1  Display Account Info" +

        "\n\t0  Quit\n\n\n";

do {
  System.out.println(menu);
  System.out.println("\tEnter your selection:  ");
  option = scan.nextInt();
  switch (option) {

    case 1: //  Enter and Validate SSN
    System.out.print("option 1");
    break;

    case 2:     //Enter and Validate Passwords
    System.out.print("option 2");
    break;

    case 3:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 3");
    break;

    case 4: //  Enter and Validate SSN
    System.out.print("option 4");
    break;

    case 5:     //Enter and Validate Passwords
    System.out.print("option 5");
    break;

    case 6:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 6");
    break;

    case 7:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 7");
    break;

    default: outputString = "\nInvalid Selection\n";
    System.out.println(outputString);
    break;

  } while (option != 0)


}

您的代码不断循环,因为当您处于循环状态时, option值永远不会改变。 您需要以不同的方式编写逻辑。 顺便说一句, switch不循环, while循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM