java - 我的布尔变量无法解析为我的 while 语句中的变量
问题描述
我想制作一个重复自己的代码,直到用户输入单词 Game 或单词 Balance。我做了一个do while
循环,但我的 while 语句出现错误。错误是:error3 cannot be resolved into a variable
。有谁知道我的代码有什么问题?
System.out.println("welcome to Roll the Dice!");
System.out.println("What is your name?");
Scanner input = new Scanner(System. in );
String Name = input.nextLine();
System.out.println("Welcome " + Name + "!");
System.out.println("Do you want to play a game or do you want to check your account's balance?");
System.out.println("For the game typ: Game. For you accounts balance typ: Balance");
do {
String Choice = input.nextLine();
String Balance = "Balance";
String Game = "Game";
input.close();
boolean error1 = !new String(Choice).equals(Game);
boolean error2 = !new String(Choice).equals(Balance);
boolean error3 = (error2 || error1) == true;
if (new String(Choice).equals(Game)) {
System.out.println("Start the game!");
}
else if (new String(Choice).equals(Balance)) {
System.out.println("Check the balance");
}
else {
System.out.println("This is not an correct answer");
System.out.println("Typ: Game to start a game. Typ: Balance to see your account's balance");
}
}
while ( error3 == true );
解决方案
error3
在do
范围内定义。将其声明移出do
范围并将值设置在内部:
boolean error3 = false;
do {
String Choice = input.nextLine();
String Balance = "Balance";
String Game = "Game";
input.close();
boolean error1 = ! new String(Choice).equals(Game);
boolean error2 = ! new String(Choice).equals(Balance);
error3 = error2 || error1;
另请注意,您可以简化(error2 || error1) == true
为简单error2 || error1
的 . while
您的陈述也可以这样做:
while(error3);
推荐阅读
- html - 字体真棒图标在 HTML 中不起作用
- plot - Mathematica - 导出多个绘图,每个绘图都有不同的名称(取决于绘图中使用的变量)
- python - 根据另一个数据帧的行值对一个数据帧中的列求和
- php - 插入期间导致 SQL 错误的模型特征
- python - 你能用参数元组创建方法吗?
- java - 无法在java中正确传递参数,只有String可以正常工作
- python - Python OpenCV cv2.waitKey(1) 导致视频窗口冻结/无响应
- android - MockWebServer Android 测试在 CI 上失败
- sql - 如何在不使用 SQL 中的多个连接条件的情况下获取同一行中的所有值?
- ios - Flutter Firebase Auth with Apple 不会注销