2

I need nested while loops to print so that the outter loop k makes one decrement iteration every time that inner loop "I" finishes an iteration. K has to start at 5 and count back to 1 and I has to starts at 0 and count to 10 by 2's. So it would the out put would look like this:

K = 5 I = 0
K = 5 I = 2
K = 5 I = 4
K = 5 I = 6
K = 5 I = 8
K = 5 I = 10
K = 4 I = 0

I have been stumped for hours and tried everyway to I can think of to make it work. Can someone please help?

public class Task1 {
public static int i = 0;
public static int k = 5;

public static void Display(){

    while(k > 1){

        while(i >=10){
            i = i+2;
            System.out.println("K = " + k + " I = " + i);

        }


        if (i >= 10){
            k=k-1;
        }
    }
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    Display();

}
    }
1
  • 1
    Why use while loops? This is normally done as for( int i = 5; i > 0; i-- ) for( int j = 0; j <= 10; j = j+2 ) System.our.println( i + " " + j); of course making same with whole loops is trivial if you have seen that a for loop is very much like a while loop. Commented Mar 14, 2015 at 20:26

7 Answers 7

3

There are a few errors. Follow the comments

while(k >= 1){                                        // Make it >= instead of >

    i = 0;                                            // reset i
    while(i <=10){                                    // Make it i<= (less than)
        System.out.println("K = " + k + " I = " + i); //print before incrementing
        i = i+2;

    }

Rest of the code is correct. And the output is as expected

K = 5 I = 0
K = 5 I = 2
K = 5 I = 4
K = 5 I = 6
K = 5 I = 8
K = 5 I = 10
K = 4 I = 0
K = 4 I = 2
K = 4 I = 4
K = 4 I = 6
.
.
.
Sign up to request clarification or add additional context in comments.

3 Comments

this works but for some reason k is stopping before it goes to 1 and prints the last iteration. any idea why? thanks for all of the help
@user2796618 Just make it >= in the while loop. Check the edit
nevermind I figured it out. I needed an = in the first while thanks for helping everyone
1

You need to initialize i before the start of the inner loop, inside the outer loop, so it gets reset each time

2 Comments

Now my out put looks like this : K = 5 I = 2 K = 5 I = 4 K = 5 I = 6 K = 5 I = 8 K = 5 I = 10 K = 4 I = 12 K = 3 I = 2 K = 3 I = 4 K = 3 I = 6 K = 3 I = 8 K = 3 I = 10 K = 2 I = 12
Note that every I printed is 2 too big; I'll leave fixing that as an exercise.
0
while(k > 1) {
   i = 0;
   while( i <= 10) { 
     Print
      i += 2;
   }
   k--;
 }

Your inner while loop condition was incorrect. And you were adding to i too soon so you would never print for 0.

Comments

0

Something like this

public class Task1 {
public static int i = 0;
public static int k = 5;

public static void Display(){

    while(k > 0){
        i = 0;
        while(i <= 10){
            System.out.println("K = " + k + " I = " + i);
            i+=2;
        }
        k--;

    }
}

public static void main(String[] args) {
    Display();

}
}

1 Comment

Your inner while loop should be less than or equal to.
0

Replace your display method with this..

public static void Display(){

       while(k > 1){
          i=0; //initialise i back to 0
          while(i <=10){ //Change >= to <=
               i = i+2;
               System.out.println("K = " + k + " I = " + i);
          }
         k--;
        //comment or delete below if block
        /*if (i >= 10){
           k=k-1;
        }*/
    }
 }

Comments

0

use the following code.

public class Task1 {

public static int i = 0;
public static int k = 5;

public static void Display() {

    while (k >= 1) { // use >=1 if you wanna print till 1
        i = 0; // set i=0
        while (i <= 10) {
            System.out.println("K = " + k + " I = " + i); // add print
                                                            // statement
            i = i + 2;

        }
        k--; // no need to use if (i >= 10){ }

    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Display();

}

}

Comments

-1

You can do it like this:

for(int k = 5;k>0;k--;){
for(int i = 0;i<10;i+=2;)
 System.out.println(k+ "" +i)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.