[英]Program bypasses while loop with multiple conditions (java)
I have this really simple code: 我有这个非常简单的代码:
public class ArrayIO {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number from 50 to 150: ");
int elements = sc.nextInt();
while(elements<50 && elements>150) {
System.out.println("Less than 50 or more than 150");
elements = sc.nextInt();
}
sc.close();
ArrayList<Integer> integers = new ArrayList<>();
for(int i=1; i<=elements; i++) {
integers.add(i);
}
integers.toArray();
System.out.printf("Array size is %d\n", integers.size());
}
}
but my program bypasses the while loop completely when it has more than one condition. 但是我的程序在有多个条件时会完全绕过while循环。 It works just fine with only one condition.
仅在一种情况下它可以正常工作。 I feel like I'm doing some really stupid mistake, but I just can't quite get it for almost an hour already.
我感觉自己犯了一些非常愚蠢的错误,但是我已经快一个小时不能理解了。
Change &&
to ||
将
&&
更改为||
if you want OR : 如果您想或 :
while(elements<50 || elements>150) {
System.out.println("Less than 50 or more than 150");
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.