我正在使用带有 do while 循环的 if 条件语句。我希望每次编译器运行程序时,它都应该打印“IF ”中的语句,直到它达到小于 15打印一次?我哪里错了。如果有人解释流程,我将不胜感激,因为我是初学者。请参阅下面的代码:
package loopexamples2;
public class DoWhileLoop {
public static void main(String[] args) {
int n = 1;
if(n<15)
System.out.println("print value of n is equal to"+ n);
do {
System.out.println(n);
n++;
}
while(n<10);
}
}
output--> 打印n的值等于1 1个 2个 3个 4个 5个 6个 7 8个 9
最佳答案
您的 if 语句在您的 do while 循环之外。将它放在 do {} block 中
package loopexamples2;
public class DoWhileLoop {
public static void main(String[] args) {
int n = 1;
do {
if(n<15) {
System.out.println("print value of n is equal to"+ n);
}
System.out.println(n);
n++;
}
while(n<10);
}
}
关于java - 使用 IF 的 do-while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170126/