开发者

Switch in Java is looping

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.开发者_Python百科print("option 7");
                        break;

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

        }
    }


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();
    ... 
}


Option is never changing in the while loop you have -- resulting in an endless loop if you type in anything that's not 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)


}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜