Переключатель в Java зациклен - PullRequest
1 голос
/ 31 августа 2011

Мой оператор 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;

        }
    }

Ответы [ 4 ]

7 голосов
/ 31 августа 2011

Я уверен, что ваш цикл while делает цикл. И вы никогда не меняете значение option в теле цикла, поэтому, конечно, оно работает непрерывно.

Предположительно, вы хотите переместить строку:

option = scan.nextInt();

К первой строке цикла:

while (option != 0) {
    option = scan.nextInt();
    ... 
}
2 голосов
/ 31 августа 2011

Параметр никогда не меняется в имеющемся цикле while, что приводит к бесконечному циклу, если вы наберете что-то, что не 0

1 голос
/ 31 августа 2011

Цикл 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)


}
0 голосов
/ 31 августа 2011

Ваш код продолжает цикл, потому что значение option никогда не меняется, когда вы находитесь в цикле.Вы должны написать свою логику по-другому.Кстати, switch не зацикливается, while зацикливается.

...