如何制作 do-loop 条件 "when string does not equal"
How to make do-loop conditions "when string does not equal"
我想让我的 do 循环 运行 而用户输入的内容不等于所需的字母 (a-i) 出于某种原因,即使我输入了正确的字母,它也会永远循环。
我试过在比较中使用 switch case 和 !=。
这是我的代码:
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=in.nextLine();
if (!lL1.equals("a")||!lL1.equals("b")||!lL1.equals("c")||!lL1.equals("d")||!lL1.equals("e")||!lL1.equals("f")||!lL1.equals("g")||!lL1.equals("h")||!lL1.equals("i")){
System.out.println("Invalid Input. Try again.");
}//End if statement
}while(!lL1.equals("a") || !lL1.equals("b") || !lL1.equals("c") || !lL1.equals("d") || !lL1.equals("e") || !lL1.equals("f") || !lL1.equals("g") || !lL1.equals("h") || !lL1.equals("i"));
我在 Java 方面的技能有限,但这应该有用,除非我遗漏了一些明显的东西。任何帮助都会很棒!
如果你有一个否定你需要AND加入条件不是OR。
那是因为如果你或一些not-ed值,它们形成和.
让我解释得更好。
如果输入a,则第一个为假(因为你不是),其他为真,所以或条件使结果为真.
您应该改为将所有 or 分组,而不是分组。
例如
!(lL1.equals("a") || lL1.equals("b") || lL1.equals("c") || lL1.equals("d") || lL1.equals("e") || lL1.equals("f") || lL1.equals("g") || lL1.equals("h") || lL1.equals("i"))
您可能想要创建一个已接受答案的列表,而不是对每个输入案例都使用一个运算符,然后您的条件将如下所示:
while answer is not in accepted answers, ask another input
例如:
Scanner scanner = new Scanner(System.in);
List<String> acceptedAnswers = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i");
String input;
do {
System.out.println(
"Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
input = scanner.nextLine();
} while (!acceptedAnswers.contains(input));
scanner.close();
System.out.println("Got correct input: " + input);
请试试这个:
char lL1;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=scanner.next().charAt(0);
}while(lL1!='a' && lL1!='b' && lL1!='c' && lL1!='d' && lL1!='e' && lL1!='f' && lL1!='g' && lL1!='h' && lL1!='i');
由于您只得到一个字符,您可以检查它是否不匹配上面显示的 [a 到 i] 个字符。这是将检查作为循环条件的最短方法。如果失败,则将调用循环。
我想让我的 do 循环 运行 而用户输入的内容不等于所需的字母 (a-i) 出于某种原因,即使我输入了正确的字母,它也会永远循环。
我试过在比较中使用 switch case 和 !=。
这是我的代码:
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=in.nextLine();
if (!lL1.equals("a")||!lL1.equals("b")||!lL1.equals("c")||!lL1.equals("d")||!lL1.equals("e")||!lL1.equals("f")||!lL1.equals("g")||!lL1.equals("h")||!lL1.equals("i")){
System.out.println("Invalid Input. Try again.");
}//End if statement
}while(!lL1.equals("a") || !lL1.equals("b") || !lL1.equals("c") || !lL1.equals("d") || !lL1.equals("e") || !lL1.equals("f") || !lL1.equals("g") || !lL1.equals("h") || !lL1.equals("i"));
我在 Java 方面的技能有限,但这应该有用,除非我遗漏了一些明显的东西。任何帮助都会很棒!
如果你有一个否定你需要AND加入条件不是OR。
那是因为如果你或一些not-ed值,它们形成和. 让我解释得更好。
如果输入a,则第一个为假(因为你不是),其他为真,所以或条件使结果为真.
您应该改为将所有 or 分组,而不是分组。
例如
!(lL1.equals("a") || lL1.equals("b") || lL1.equals("c") || lL1.equals("d") || lL1.equals("e") || lL1.equals("f") || lL1.equals("g") || lL1.equals("h") || lL1.equals("i"))
您可能想要创建一个已接受答案的列表,而不是对每个输入案例都使用一个运算符,然后您的条件将如下所示:
while answer is not in accepted answers, ask another input
例如:
Scanner scanner = new Scanner(System.in);
List<String> acceptedAnswers = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i");
String input;
do {
System.out.println(
"Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
input = scanner.nextLine();
} while (!acceptedAnswers.contains(input));
scanner.close();
System.out.println("Got correct input: " + input);
请试试这个:
char lL1;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("Please enter the location of your battleship, starting with the first letter value. Make sure it is from the letters a-i.");
lL1=scanner.next().charAt(0);
}while(lL1!='a' && lL1!='b' && lL1!='c' && lL1!='d' && lL1!='e' && lL1!='f' && lL1!='g' && lL1!='h' && lL1!='i');
由于您只得到一个字符,您可以检查它是否不匹配上面显示的 [a 到 i] 个字符。这是将检查作为循环条件的最短方法。如果失败,则将调用循环。