简体   繁体   中英

Java won't print inside a for loop

I've been toying around with Java recently and I've encountered a problem:

Whenever I put System.out.println() or System.out.print() inside a for loop, there is no output on the console.

For example:

package experimental;

public class Main {
    public static void main(String args[]) {
        recur();
    }

    public static void recur() {
        for(int x = 0; x == 10; x++) {
            for(int y = 0; y == 10; y++) {
                System.out.print(x + "-" + y + " ");
            }
        }
    }
}

Outputs:

*crickets*

The loop condition isn't true to even begin with... x == 10 is not true when x = 0 . Had it been, you would've also noticed the second loop condition is also not true to begin with; similarly , y == 10 is not true as y = 0 . You probably confused the condition with one for stopping .

for(int x = 0; x == 10; x++) {
     for(int y = 0; y == 10; y++) {

Change to the following if you want x , y to vary from [0, 10):

for(int x = 0; x < 10; x++) {
    for(int y = 0; y < 10; y++) {

... or the following for [0, 10]:

for(int x = 0; x <= 10; x++) {
    for(int y = 0; y <= 10; y++) {

Your problem is with the condition in your for loop. It says:

start with x = 0 and loop while x is 10

That is not true so the loop exits immediately.

You need to replace it with:

for (int x = 0; x < 10; x++)

(assuming you want to loop 10 times)

Those for loops will never loop.

They'll only loop so long as the termination condition is true . As termination conditions you have: x == 10 , y == 10 . Since you initialize x , y to 0 , these conditions are never true, and the loops are a no-op.

I think you want, eg: for (int x = 0; x < 10; x++)

for(int x = 0; x == 10; x++) {
    for(int y = 0; y == 10; y++) {
        System.out.print(x + "-" + y + " ");
    }
}

The x == 10 condition will fail, the loop will break, crickets. Same with the other loop condition.

Your code never gets to the body of the for loop because the condition x==10 is already false before it starts the loop. I think you should be writing x != 10 instead. Same goes for the inner loop.

Since everyone has already answered your question, I would like to add some debugging tips. First of all, I suggest using an IDE such as NetBeans or Eclipse. The debuggers in both of these will allow you to step through your code line by line to see how it executes. In doing so, you would have quickly noticed that your for loops are skipped completely. If you are unable or unwilling to do this. Then the next best thing is to add SOP's ( System.out.println() ) in your code. Even if it's just a simple message such as "Here" to tell you where you are in the code, this additional output can help you track down errors in logic.

You can't use x == 10 . The comparison needs to express this way:

for(int x = 0; x <= 10; x++) {
   for(int y = 0; y <= 10; y++) {
     System.out.print(x + "-" + y + " ");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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