Cory
Cory

Reputation: 35

Struggling with nested for loops

Alright so, I'm new to nested for loops adn I'm having a bit of an issue on understanding them. I've read many guides, but I still don't fully understand.

Alright the prompt:

Write nested for loops that produce the following output:

000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999

What I have so far

for(int num2 = 0; num2 <= 9;  num2++) {

      for(int num1 = 0; num1 <= 2; num1++) {

            System.out.println(num2 + " " + num2 + " " + num2);
      }
}      

And the output is

0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9

What am I doing wrong?

Upvotes: 1

Views: 2663

Answers (6)

Code Whisperer
Code Whisperer

Reputation: 1041

You got 3 copies of each number.

the outer loop:

for (int i = 0; i < 10; i++) {

chooses which number you want to print so that is fine.

The inner loop however is comparing j against the chosen number. You want 3 copies, not a variable number of copies. This change will make 3 copies:

for (int j = 0; j < 3; j++) {

You also don't need this:

System.out.println(i);

EDIT: I just noticed you need 3 of these outputs.

add an outer loop:

for (int x = 0; x < 3; x++) {

and a blank space

System.out.println(" ");

So the final result should be:

for (int x = 0; x < 3; j++) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(i);
        }
    }
    System.out.println(" ");
}

Upvotes: 3

Bob Masinburg
Bob Masinburg

Reputation: 11

for(int k=0;k<3;k++) {  
   for (int i = 0; i< 10; i++) {
       for (int j = 0; j <  3;j++) {
            System.out.println(i);   
        }
    }
}

Although I am by far most inexperienced guy here, I think this should give exact output you're looking for.

Upvotes: -1

J0e3gan
J0e3gan

Reputation: 8938

Try this:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 3; k++) {
            System.out.print(j);
        }
    }
    System.out.println("");
}
  • loop 1: You want the sequence 3 times, each occurrence on its own line.
  • loop 2: You want the sequence to have digits 0 through 9 ascendingly.
  • loop 3: You want the sequence to have each digit in succession 3 times.

Upvotes: 0

Abhishek Prakash
Abhishek Prakash

Reputation: 171

In the program provided by you the following events take place:-

  1. In first loop variable i is initiated, condition for loop is checked and then it moves to the second loop if condition is true.
  2. Now second loop iterates over the value of j until the condition is false and then control returns to the first loop.

Try to follow the working of loop and you can see yourself where were you wrong.

Upvotes: 0

Kunjan Thadani
Kunjan Thadani

Reputation: 1670

for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 10; j++) {
            System.out.print(j + "" + j + "" + j);
        }
        System.out.println();
    }

Upvotes: 1

cronos2546
cronos2546

Reputation: 1106

for (int k = 0; k<3, k++){
    for (int i = 0; i< 10; i++) {
        for (int j = 0; j <  3;j++) {

            System.out.println(i);   
        }
        }
        System.out.println("")};

    }
    }

Upvotes: -1

Related Questions