Java中的Switch是循环的
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很确定是您的
while
循环正在执行循环。而且您永远不会更改循环体内option
的值,因此它当然会连续运行。据推测,您想要将行: 移动
到循环的第一行:
I'm pretty sure it's your
while
loop that is doing the looping. And you are never changing the value ofoption
within the body of the loop, so of course it runs continuously.Presumably, you want to move the line:
To the first line of the loop:
选项在 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
do while 循环需要更少的代码:
A do while loop would require less code:
您的代码会继续循环,因为当您处于循环中时
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.