【发布时间】:2016-06-04 11:14:33
【问题描述】:
我在我的代码中创建了一个主菜单。当用户键入 q 我希望代码停止运行。相反,它只是再次显示菜单,我应该使用什么代码来阻止它。如果你按任何其他字母它就可以了。谢谢你
import java.util.Random;
public class Aaa {
AQAConsole2016 console = new AQAConsole2016();
Random random = new Random();
int boardSize;
boolean moveIsValid;
char [][] board;
int move;
char choice;
String playerName="Human";
String player2="Computer";
public Aaa() {
boardSize = 6;
do {
displayMenu();
choice = getMenuChoice(playerName);
switch (choice) {
case 'p' : playGame(playerName, boardSize);
break;
case 'e' : playerName = getPlayersName();
break;
case 'c' : boardSize = changeBoardSize();
break;
case 'm' : Multiplayer( boardSize,playerName,player2);
break;
case 'r' :readBoard(board,boardSize);
break;
case 'q' : quit();
}
}while (choice!='p'||choice!='e'||choice!='c'||choice!='m'||choice!='r'||choice!='q');
}
void quit(){
}
【问题讨论】:
-
您不需要
quit方法,只需在while循环中多花点心思,为什么while (choice!='p'||choice!='e'||choice!='c'||choice!='m'||choice!='r'||choice!='q')将永远是正确的。 -
@调试 while 循环是无止境的。查看boolean algebra 以了解您在while 循环条件下犯的错误。只需跳出循环就足以退出。
-
我投票决定将此问题作为离题结束,因为它过于本地化。这里的问题也应该帮助其他人,而不仅仅是提问者
标签: java eclipse case do-while void