逻辑或在 while 循环中不能正常工作
Logical OR does not work properly in the while loop
问题是,while 循环中的第一个条件即使为真也根本不会执行。如果我从 while 循环中删除逻辑或并只写第一个条件 (selection.compareToIgnoreCase("O") >0
) 它工作正常。但是如果有两个条件用逻辑或就不行了。
我试过使用equals()
,我也试过使用否定逻辑
while(!selection.equals("O") || !selection.equals("E"))
。第二个条件可以正常工作,但第一个条件根本不起作用。
public class OddsOrEvens {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Let’s play a game called \"Odds and Evens\"");
System.out.println("Whats your name ? ");
String name = sc.nextLine();
System.out.println("Hi "+ name +", which do you choose? (O)dds or (E)vens?");
String selection = sc.nextLine();
System.out.println("selection: " + selection);
while (selection.compareToIgnoreCase("O") >0 || selection.compareToIgnoreCase("E") >0){
System.out.println("Please enter the correct choice. Select 'O' for odds or 'E' for evens");
selection = sc.next();
}
if(selection.equalsIgnoreCase("O")){
System.out.println(name + " has picked Odds! The computer will be evens.");
}else if (selection.equalsIgnoreCase("E")){
System.out.println(name + " has picked Evens! The computer will be Odds.");
}
}
}
您的字符串比较不正确。比较 returns -1/0/1 for less/equal/greater.
更清晰的方法是使用 toUppercase().equals(...
.
while (!selection.toUpperCase().equals("O") && !selection.toUpperCase().equals("E")){
那是因为 不适用于两种情况 ,一个需要 !... && ! ...
OR ||
会产生始终为真的效果,如至少有一个案例是错误的。或者 !(... || ...)
.
while (!selection.equalsIgnoreCase("O") && !selection.equalsIgnoreCase("E")) {
让我们简化一下:
!(n == 1) || !(n == 2) // WRONG
n != 1 || n != 2 // WRONG
将永远为真,因为 n == 1 为假或 n == 2 为假:至多一个选择可以为真,而其他选择为假。所以on at least on side是!false,true,所以整个表达式都是true。
!(n == 1) && !(n == 2) // GOOD
n != 1 && n != 2 // GOOD
心理上的错误是语言上的OR大多是异或的意思。
可能等价于:
!(n == 1 || n == 2) <=> n != 1 && n != 2 [De Morgan's law]
问题是,while 循环中的第一个条件即使为真也根本不会执行。如果我从 while 循环中删除逻辑或并只写第一个条件 (selection.compareToIgnoreCase("O") >0
) 它工作正常。但是如果有两个条件用逻辑或就不行了。
我试过使用equals()
,我也试过使用否定逻辑
while(!selection.equals("O") || !selection.equals("E"))
。第二个条件可以正常工作,但第一个条件根本不起作用。
public class OddsOrEvens {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Let’s play a game called \"Odds and Evens\"");
System.out.println("Whats your name ? ");
String name = sc.nextLine();
System.out.println("Hi "+ name +", which do you choose? (O)dds or (E)vens?");
String selection = sc.nextLine();
System.out.println("selection: " + selection);
while (selection.compareToIgnoreCase("O") >0 || selection.compareToIgnoreCase("E") >0){
System.out.println("Please enter the correct choice. Select 'O' for odds or 'E' for evens");
selection = sc.next();
}
if(selection.equalsIgnoreCase("O")){
System.out.println(name + " has picked Odds! The computer will be evens.");
}else if (selection.equalsIgnoreCase("E")){
System.out.println(name + " has picked Evens! The computer will be Odds.");
}
}
}
您的字符串比较不正确。比较 returns -1/0/1 for less/equal/greater.
更清晰的方法是使用 toUppercase().equals(...
.
while (!selection.toUpperCase().equals("O") && !selection.toUpperCase().equals("E")){
那是因为 不适用于两种情况 ,一个需要 !... && ! ...
OR ||
会产生始终为真的效果,如至少有一个案例是错误的。或者 !(... || ...)
.
while (!selection.equalsIgnoreCase("O") && !selection.equalsIgnoreCase("E")) {
让我们简化一下:
!(n == 1) || !(n == 2) // WRONG
n != 1 || n != 2 // WRONG
将永远为真,因为 n == 1 为假或 n == 2 为假:至多一个选择可以为真,而其他选择为假。所以on at least on side是!false,true,所以整个表达式都是true。
!(n == 1) && !(n == 2) // GOOD
n != 1 && n != 2 // GOOD
心理上的错误是语言上的OR大多是异或的意思。
可能等价于:
!(n == 1 || n == 2) <=> n != 1 && n != 2 [De Morgan's law]