Java 带异常的For循环
如何为循环编写一个异常的Java 带异常的For循环,java,for-loop,Java,For Loop,如何为循环编写一个异常的。我真的不想使用数组。例如,我如何编写一个For循环来打印从1到10的计数,但遗漏了5 此代码如下: class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } } 如何使其输出:(遗漏5) 你试过什么
。我真的不想使用数组。例如,我如何编写一个For循环来打印从1到10的计数,但遗漏了5
此代码如下:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
如何使其输出:(遗漏5)
你试过什么?这里有一个可能的解决方案:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
if(i != 5){
System.out.println("Count is: " + i);
}
}
}
}
class-ForDemo{
公共静态void main(字符串[]args){
对于(inti=1;你是在要求一个if
语句吗?你是什么意思?if=5
不要打印?嗯,这听起来不是一个好方法吗?你需要自己思考。IntStream.rangeClosed(1,10)。filter(i->i!=5)。forEach(i->System.out.println(“Count is:+i”);
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
if(i != 5){
System.out.println("Count is: " + i);
}
}
}
}