Java中的无止境while循环
这是一个无限循环:Java中的无止境while循环,java,loops,while-loop,Java,Loops,While Loop,这是一个无限循环: int option=0; while (option1!=1 || option1!=2){ System.out.println("Give 1 for the first list which includes what we have in our exhibition"); System.out.print("and 2 which we have not:"); // 2 print because i wa
int option=0;
while (option1!=1 || option1!=2){
System.out.println("Give 1 for the first list which includes what we have in our exhibition");
System.out.print("and 2 which we have not:"); // 2 print because i want to show at two different lines
option1= Integer.parseInt(in.nextLine());} // when i give 1 or 2 as an option it doesn't goes out frome the loop
由于选项1
永远不能同时等于1
和2
,因此该条件将始终计算为true
,循环将始终继续。您可能打算在比较中使用逻辑“and”运算符(&&&
):
while(option1!=1 || option1!=2)
这样,如果
option1
等于这两个选项中的一个,循环就会结束。你需要说option1=1&选项1=2
,而不是选项1=1 | |选项1=2
请记住格式化代码,使其可读。只需在每行前面添加四个空格。我想重复,直到用户给出数字1或2
选项!=1 | |选项!=2
总是正确的,您需要使用&
而不是|
。
while(option1!=1 && option1!=2)