Java中的Switch是循环的

发布于 2024-12-02 14:22:56 字数 1732 浏览 0 评论 0原文

我的 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;

        }
    }

My switch statement just keeps looping. It should print the option you choose and then reprint the menu. Please help!
Here is my code:

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;

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╭⌒浅淡时光〆 2024-12-09 14:22:56

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

据推测,您想要将行: 移动

option = scan.nextInt();

到循环的第一行:

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

I'm pretty sure it's your while loop that is doing the looping. And you are never changing the value of option within the body of the loop, so of course it runs continuously.

Presumably, you want to move the line:

option = scan.nextInt();

To the first line of the loop:

while (option != 0) {
    option = scan.nextInt();
    ... 
}
心清如水 2024-12-09 14:22:56

选项在 while 循环中永远不会改变——如果您输入任何非 0 的内容,则会导致无限循环

Option is never changing in the while loop you have -- resulting in an endless loop if you type in anything that's not 0

阳光下的泡沫是彩色的 2024-12-09 14:22:56

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)


}

A do while loop would require less code:

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)


}
复古式 2024-12-09 14:22:56

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

Your code keeps on looping because value of option never changes when you are in the loop. You need to write your logic differently. Btw, switch does not loop, while does the looping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文