Click here to Skip to main content
15,903,632 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear all,

can anyone plz help me to design java trangle using while loop as below

*
* *
* * *
* * * *

in this way i need to design i try to design it but it is showing infinite.

this is my code

C#
class Ex4{

    public static void main(String [] san)
    {
        int z=1;

        while(z<7){

            for(int j=0;j<=z;j++)
                            {
                for(int i=0;i<=j;i++)
                                 {
                                   System.out.println("*");

                                  z++;
                                }

                               System.out.print("\n");
                           }
        }

    }

}
Posted

1 solution

I cannot see where you could use 7. Besides, the problem requires two nested loops, but you use three.

The problem is very simple. The idea of this exercise is: you should be able to solve such small problems very quickly, from understanding the problem to having fully debugged functional code. Here is one simple advice: rename all variables to give then some semantic names, expressed in English words without abbreviations, like length, horizontalIndex, lineCount, line. And format the code properly, show nesting strictly according its logical structure. It will help you to see what the code is doing, immediately.

The rest of it is too simple to discuss any further. I leave completion for your home exercises. Remember that your school gives you a chance to learn something; and you would loose this chance if someone gives you a complete solution.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900