我正在寻找这个无限循环的替代方案
I'm looking for an alternative to this Infinite Loop
我在下面的代码中使用了一个 while(true) 循环,但我们已经停止使用它了。我想不出另一种方法。
我试过使用 do-while 循环,但这对我的情况没有帮助。
'''java
while(true){
System.out.println("\nSelect the number of the Option you wish to carry out:\n 1) Enter Scores\n 2) Find Golfer\n 3) Display Scoreboard\n 4) Edit Scoresheet\n 5) Exit\n ");
userChoice = integerVerify(); //Used to verify whether user input is a valid number
switch (userChoice) {
case 1:
System.out.println("Please enter the scores in the following order");
displayPlayers(); //Displays scoreboard to help users enter player scores in order.
addScores(); //Used to verify whether user input is a valid String
break;
case 2:
System.out.println("**********PLEASE ENTER THE NAME OF THE PLAYER YOU WISH TO FIND**********");
findPlayer();
break;
case 3:
displayPlayers();
break;
case 4:
options();
break;
case 5:
System.out.println("Are you sure you wish to exit?");
confirm = stringVerify();
if (confirm.equalsIgnoreCase("yes") || confirm.equalsIgnoreCase("y")) {
System.out.println("Thank you for using our application.");
System.out.println("Exiting");
System.exit(0);
}
break;
default:
System.out.println("Please enter an appropriate option.");
}
}
'''
代码需要拒绝任何不在 switch-case 中的东西...但它还需要显示适当的消息,无论是通过函数还是来自循环本身,最终,我仍然需要它来循环直到输入退出选项(案例 5)。
大多数长 运行 系统都有某种顶级 "Infinite" 循环。我不认为这有什么大问题,但在政治上有些人不喜欢无限循环。
如果这是您的问题,请将布尔 "running" 标志初始化为 true,使用 while(运行) 而不是 System.exit() 设置 运行 false。应该是一样的效果。
public static void main(String[] s)
{
Boolean running=true;
while(running) {
switch() {
...
case 5:
...
if(exitConditionsMet)
running=false;
…
}
}
return; // Just let main return to exit program.
}
从技术上讲,没有真正的区别,但有些人已经接受过扫描 while(true) 构造的培训,并将其称为问题。
标志方法有一些细微的优势...
- 通过函数退出的控制流程是意外的。如果您在非常高的级别扫描该代码只是为了寻找流控制(大括号和 if/while/for/break 类型构造),您不会立即看到该循环将永远退出。
- 出于同样的原因,一些静态分析工具可能会有些混乱。
- System.exit 通常应该避免(作为习惯问题,在这种情况下不具体)。 System.exit 可以强制容器(如 Tomcat)异常关闭,它也可以杀死可能正在做一些重要事情的线程(除非你需要 return 一个值到命令行,这意味着您需要一个 system.exit() 但可能希望将其作为 main.
的最后一行
我在下面的代码中使用了一个 while(true) 循环,但我们已经停止使用它了。我想不出另一种方法。
我试过使用 do-while 循环,但这对我的情况没有帮助。
'''java
while(true){
System.out.println("\nSelect the number of the Option you wish to carry out:\n 1) Enter Scores\n 2) Find Golfer\n 3) Display Scoreboard\n 4) Edit Scoresheet\n 5) Exit\n ");
userChoice = integerVerify(); //Used to verify whether user input is a valid number
switch (userChoice) {
case 1:
System.out.println("Please enter the scores in the following order");
displayPlayers(); //Displays scoreboard to help users enter player scores in order.
addScores(); //Used to verify whether user input is a valid String
break;
case 2:
System.out.println("**********PLEASE ENTER THE NAME OF THE PLAYER YOU WISH TO FIND**********");
findPlayer();
break;
case 3:
displayPlayers();
break;
case 4:
options();
break;
case 5:
System.out.println("Are you sure you wish to exit?");
confirm = stringVerify();
if (confirm.equalsIgnoreCase("yes") || confirm.equalsIgnoreCase("y")) {
System.out.println("Thank you for using our application.");
System.out.println("Exiting");
System.exit(0);
}
break;
default:
System.out.println("Please enter an appropriate option.");
}
}
'''
代码需要拒绝任何不在 switch-case 中的东西...但它还需要显示适当的消息,无论是通过函数还是来自循环本身,最终,我仍然需要它来循环直到输入退出选项(案例 5)。
大多数长 运行 系统都有某种顶级 "Infinite" 循环。我不认为这有什么大问题,但在政治上有些人不喜欢无限循环。
如果这是您的问题,请将布尔 "running" 标志初始化为 true,使用 while(运行) 而不是 System.exit() 设置 运行 false。应该是一样的效果。
public static void main(String[] s)
{
Boolean running=true;
while(running) {
switch() {
...
case 5:
...
if(exitConditionsMet)
running=false;
…
}
}
return; // Just let main return to exit program.
}
从技术上讲,没有真正的区别,但有些人已经接受过扫描 while(true) 构造的培训,并将其称为问题。
标志方法有一些细微的优势...
- 通过函数退出的控制流程是意外的。如果您在非常高的级别扫描该代码只是为了寻找流控制(大括号和 if/while/for/break 类型构造),您不会立即看到该循环将永远退出。
- 出于同样的原因,一些静态分析工具可能会有些混乱。
- System.exit 通常应该避免(作为习惯问题,在这种情况下不具体)。 System.exit 可以强制容器(如 Tomcat)异常关闭,它也可以杀死可能正在做一些重要事情的线程(除非你需要 return 一个值到命令行,这意味着您需要一个 system.exit() 但可能希望将其作为 main. 的最后一行