2

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*
4

7 回答 7

9

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++) {
于 2012-08-08T23:23:07.823 回答
3

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)

于 2012-08-08T23:23:42.737 回答
2

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, e.g.: for (int x = 0; x < 10; x++)

于 2012-08-08T23:23:17.217 回答
2
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.

于 2012-08-08T23:23:22.343 回答
1

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.

于 2012-08-08T23:27:20.437 回答
0

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.

于 2012-08-08T23:33:18.700 回答
0

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 + " ");
   }
}
于 2012-08-10T10:49:57.893 回答