0

I am supposed to give this output

* * * * *
 * * * * *
* * * * * *
 * * * * *

so on and so forth 5 itirations but it only shows the first 2 output

here's my code

public class itiration {

    public static void main( String args[]){

        int counter1 = 1;
        int counter2 = 1;
        int counter3 = 1;

        while(counter1<=5)
        {

                while(counter2<=5)
                {
                    System.out.print("* ");
                    System.out.print(" ");
                    counter2++;
                }

            System.out.println();

                while(counter3<=5)
                {
                    System.out.print(" ");
                    System.out.print("* ");
                    counter3++;
                }


            System.out.println();

            counter1++;
        }

    }

}

this is not a homework

2
  • In the output sample at the top of your question, the fourth line has six *s. Is that a typo or is it supposed to be like that? Commented May 19, 2011 at 4:52
  • What should the next line in your example sequence be? I'm not seeing any obvious pattern. Commented May 19, 2011 at 4:53

3 Answers 3

3

Have you tried stepping through this program with a debugger?

HINT: After the outer loop executes its first iteration, what are the values of counter2 and counter3?

Sign up to request clarification or add additional context in comments.

7 Comments

Also, if this isn't homework, then what is it? Just a self learning exercise?
OHHH THANKS! I forgot about the counter2 and counter3. :| now I learned something new. so their values doesn't reset? after counter1++ loops?
No, you need to explicitly reset the values. The best way to see this is to step through this in a debugger.
This is a just a self learning exercise, since I am currently studying java. for this coming semester and I bored to death this vacation so I decided to sefl study java I am using a book. also. how do I use the debugger?
Write Java in an IDE like Eclipse or launch jdb
|
2

You need to reset counter2 and counter3 in the loop (after counter1++ for example), otherwise they'll stay at value 5 after the first run of the loop, and the inner loops will not run any more.

Comments

1

You're not resetting counter2 and counter3 for each iteration of the main loop. Try this:

    int counter1 = 1;
    while(counter1<=5)
    {        
        int counter2 = 1;
        int counter3 = 1;

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.