gpt4 book ai didi

java - Java中的开关正在循环

转载 作者:行者123 更新时间:2023-11-29 09:50:20 25 4
gpt4 key购买 nike

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

关于java - Java中的开关正在循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com