Java equals 没有按照我期望的方式工作
Java equals does not work in the way I'd expect it to work
在我的 While 循环中,在第一个循环之后它确实跳出了 While 循环,但它没有进入我的 If 检查,有没有人看到我做错了什么?
while (!"Y".equals(inputString) && !"N".equals(inputString)) {
inputString = Helper.InputHelper().toUpperCase();
if (inputString.equals("N")) {
System.out.println("Thank you for playing!");
System.exit(0);
} else if (inputString.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
}
}
大家说的对我脑残,改成
System.out.println("Would you like to play another game? (Y/N): ");
String input = "";
while (!"Y".equals(input) && !"N".equals(input)) {
input = Helper.InputHelper().toUpperCase();
if (input.equals("N")) {
System.out.println("Thank you for playing!");
System.exit(0);
} else if (input.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
}
}
并且工作完美,谢谢。
您必须在 while 循环中重复读取用户输入。
通常这是一个 do-while-loop .
String inputString = null;
do
{
inputString = new Scanner(System.in).next();
System.out.println("inputString = " + inputString);
}
while (!"Y".equals(inputString) && !"N".equals(inputString));
while (!"Y".equals(inputString) && !"N".equals(inputString))
你的 while 循环条件有问题。
你告诉 while 如果 inputString 不等于 Y 且不等于 N 然后输入 while !
如果 inputString 等于 Y
或等于 N
.
,您应该进入循环
否则您需要用户继续输入 inputString
我打赌这就是你想要的。
Scanner input = new Scanner(System.in);
while (true) {
String answer = input.next().toUpperCase();
if (answer.equals("N")) {
System.out.println("Thank you for playing!");
// don't forget to close you scanner
input.close();
System.exit(0);
break;
} else if (answer.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
input.close();
break;
} else {
// if you don't get appropriate answer from users then warn them about it
System.out.println("Please type either Y or N");
}
}
无论哪种方式,当你遇到条件时,你应该打破,否则没有弯腰那个循环。
始终建议您使用值进行一些测试,例如:
如果输入字符串 = "N"
while 部分的计算结果为 (!"Y".equals("N") && !"N".equals("N"))
所以左边部分为真,右边部分为假,AND 使它们全部为假,而无论用户输入 N 还是 Y,while 条件都不为真。
我认为您应该使用 || 来简化 while 条件(OR) 运算符并删除 NOT (!) 运算符。
在我的 While 循环中,在第一个循环之后它确实跳出了 While 循环,但它没有进入我的 If 检查,有没有人看到我做错了什么?
while (!"Y".equals(inputString) && !"N".equals(inputString)) {
inputString = Helper.InputHelper().toUpperCase();
if (inputString.equals("N")) {
System.out.println("Thank you for playing!");
System.exit(0);
} else if (inputString.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
}
}
大家说的对我脑残,改成
System.out.println("Would you like to play another game? (Y/N): ");
String input = "";
while (!"Y".equals(input) && !"N".equals(input)) {
input = Helper.InputHelper().toUpperCase();
if (input.equals("N")) {
System.out.println("Thank you for playing!");
System.exit(0);
} else if (input.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
}
}
并且工作完美,谢谢。
您必须在 while 循环中重复读取用户输入。 通常这是一个 do-while-loop .
String inputString = null;
do
{
inputString = new Scanner(System.in).next();
System.out.println("inputString = " + inputString);
}
while (!"Y".equals(inputString) && !"N".equals(inputString));
while (!"Y".equals(inputString) && !"N".equals(inputString))
你的 while 循环条件有问题。
你告诉 while 如果 inputString 不等于 Y 且不等于 N 然后输入 while !
如果 inputString 等于 Y
或等于 N
.
否则您需要用户继续输入 inputString
我打赌这就是你想要的。
Scanner input = new Scanner(System.in);
while (true) {
String answer = input.next().toUpperCase();
if (answer.equals("N")) {
System.out.println("Thank you for playing!");
// don't forget to close you scanner
input.close();
System.exit(0);
break;
} else if (answer.equals("Y")) {
System.out.println("Starting a new game");
MineSweeper game;
game = new MineSweeper();
input.close();
break;
} else {
// if you don't get appropriate answer from users then warn them about it
System.out.println("Please type either Y or N");
}
}
无论哪种方式,当你遇到条件时,你应该打破,否则没有弯腰那个循环。
始终建议您使用值进行一些测试,例如:
如果输入字符串 = "N" while 部分的计算结果为 (!"Y".equals("N") && !"N".equals("N")) 所以左边部分为真,右边部分为假,AND 使它们全部为假,而无论用户输入 N 还是 Y,while 条件都不为真。 我认为您应该使用 || 来简化 while 条件(OR) 运算符并删除 NOT (!) 运算符。