0wendaman
0wendaman

Reputation: 43

System.out.print() is not working?

package main;

public class Counter {

public static void main(String[] args) {
    int x = 100000;

    while (x < 0) {
        x -= 7;
        System.out.print(x);
    }

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

This code is not printing on my eclipse console, but I can't find any errors or problems with my code - I expect it to print a pattern of numbers. Help is appreciated.

Answered:

package main;

public class Counter {

public static void main(String[] facepalm) {
    int x = 100000;

    while (x > 0) {
        x -= 7;
        System.out.print(x);
    }

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

Upvotes: 0

Views: 116

Answers (2)

Jobs
Jobs

Reputation: 3377

Change the '<0's in the while and for conditions to >0. Note that the body of while and for keeps running until the condition evaluates to False.

public class Counter {

public static void main(String[] args) {
int x = 100000;

while (x > 0) {
    x -= 7;
    System.out.print(x);
}

for (int y = 10; y > 0; y = y - 7) {
    System.out.print("lol");
}
}
}

Upvotes: 0

Will Hartung
Will Hartung

Reputation: 118794

Your logic is failing.

while (x < 0) {

will immediately fail, since x = 10000, which is greater than 0.

Same with the for loop.

Upvotes: 2

Related Questions