在此代码中,如果用户想再次播放,则输入“是”
我必须使用此代码的函数,我不能简单地说明while(again.equals("yes"));
。
部分代码:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
我的问题是为什么}while(running==true);
出现语法错误?我在它上面声明了变量,不应该允许它运行吗?
答案 0 :(得分:4)
你需要一个"做"首先声明,即do { statements; } while (condition)
。还
更新
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));