简体   繁体   English

使用IF的do-while循环

[英]do-while loop using IF

I am using if condition statement with a do while loop. 我正在使用带有do while循环的条件语句。 I want that everytime when the compiler runs the program it should print the statement in the" IF " till it reaches less then 15 what is happening is that the job is going quite well in do-while loop but the statement in" IF " only printed once? 我希望每当编译器运行程序时它应该在“IF”中打印语句,直到它达到少于15,发生的事情就是在do-while循环中作业进展顺利但是只在“IF”中的语句打印一次? Where am i going wrong. 我哪里错了。 If someone explains the flow it would be really appreciated as i am a beginner. 如果有人解释这个流程,我会非常感激,因为我是初学者。 Please see below for the code : 请参阅下面的代码:

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--> print value of n is equal to1 1 2 3 4 5 6 7 8 9 输出 - > n的打印值等于1 1 2 3 4 5 6 7 8 9

Your if statement is outside your do while loop. 你的if语句不在你的do while循环之外。 Put it inside the do {} block 把它放在do {}块中

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);           
    }
}

You're looking for something closer to this: 你正在寻找更接近这个的东西:

while(n<15) {
    System.out.println("print value of n is equal to"+ n);
    n++
}

The block of code within { and } will be run constantly until n reaches a value greater or equal to 15. {}的代码块将不断运行,直到n达到大于或等于15的值。

With your current code, the full print statement is only output once, as it isn't included in your while loop. 使用当前代码,完整的print语句只输出一次,因为它不包含在while循环中。

I guess this is what you want: 我想这就是你想要的:

package loopexamples2;

public class DoWhileLoop {

    public static void main(String[] args) {
        int n = 1;        
        do {
           System.out.println("print value of n is equal to"+ n);
           n++;
        } while(n<10);           
    }
}

Code flow: 代码流程:

int n = 1; Assigns n to 1 将n指定为1

do { Repeat code between braces from here until while . do {从这里括号之间重复代码,直到while

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to1 打印print value of n is equal to1

n++; Increments n by 1, so n = 2 将n递增1,因此n = 2

} while(n<10); Is n < 10? n <10? Yes so repeat from do 是的,所以重复do

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to2 打印print value of n is equal to2

n++; Increments n by 1, so n = 3 将n递增1,因此n = 3

} while(n<10); Is n < 10? n <10? n = 3 so yes, repeat from do n = 3所以是的,重复do

[ ...same thing repeats until... ] [...同样的事情重复直到...]

System.out.println("print value of n is equal to"+ n); Prints print value of n is equal to9 打印print value of n is equal to9

n++; Increments n by 1, so n = 10 将n递增1,因此n = 10

} while(n<10); Is n < 10? n <10? n = 10 so no, exit loop. n = 10所以没有,退出循环。

End of program. 程序结束。

The statement inside the IF is passed only once. IF中的语句只传递一次。 Since it is not inside any loop. 因为它不在任何循环内。

The flow of the program is procedural. 程序的流程是程序性的。 So the execution is from top to bottom. 所以执行是从上到下。 It first pass the IF block, executes it, procedes to the next line, finds the do-while block which is loop until the while condition is met. 它首先通过IF块,执行它,前进到下一行,找到do-while块,它是循环,直到满足while条件。 Loops only iterates on it's block and not on lines before it. 循环只迭代它的块而不是它之前的行。

I explain the workflow, since you are a beginner: 我解释了工作流程,因为你是初学者:

    int n = 1;

1 is assigned to n . 1分配给n

    if(n<15)
        System.out.println("print value of n is equal to"+ n);

The program check if n is less than 15. it is the case, so it execute the action: print "print value of n is equal to" + n. 程序检查n是否小于15.情况就是这样,它执行动作:print“n的打印值等于”+ n。 It only does that once. 它只做了一次。 It's not a while or a for loop that execute a loop repeatedly. 重复执行循环不是一段时间或for循环。

    do {
     System.out.println(n);
     n++;
    }
     while(n<10);           
    }

Here the program execute everything between the { and the } until n=10. 这里程序执行{和}之间的所有内容,直到n = 10。

You are saying "it should print the statement in the" IF " till it reaches less then 15" 你说“它应该在”IF“中打印语句,直到它达到15以下”

Then you need a similar do/while loop: 然后你需要一个类似的do / while循环:

 do {
     System.out.println("print value of n is equal to"+ n);
     n++
 }
 while (n<15);

I want that everytime when the compiler runs the program it should print the statement in the" IF " till it reaches less then 15 我希望每当编译器运行程序时它应该在“IF”中打印语句,直到它达到15以下

Since you are using n<10 condition in do while loop , the loop will not proceed beyond n=9 . 由于在do while循环中使用n<10条件,因此循环不会超过n=9 So it won't reach n=15 . 所以它不会达到n=15

First off you need to put the if() statement INSIDE the do while loop. 首先,你需要将if()语句INSIDE在do while循环中。 otherwise the program only sees the if once. 否则程序只看到if一次。

the next thing you're doing is in your statement: 你正在做的下一件事是在你的陈述中:

do {
    System.out.println(n);
n++;
}
while(n<10);

you have it only going until n is less then 10, you want it to be less then 15. 你只有在n小于10之前,你希望它小于15。

I assume you wan't it to print out 15 as well so you can change 我假设你不打算打印15,所以你可以改变

while(n < 10)

to

while(n <= 15)

so all together your code would look like this: 所以你的代码总是这样:

package loopexamples2;

public class DoWhileLoop {

public static void main(String[] args) {
    int n = 1;

    do {
        if(n <= 15){
            System.out.println("The value of n is equal to " + n);
            n++;
    } while(n <= 15);
}
}

note <= means "less then or equal to" 注意<=表示“小于或等于”

You can refer this any time... It will be more helpful to you.. 您可以随时参考......这对您更有帮助..

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM