Java for loop making asterisk matrix -


i've trying make form loop in java:

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

my code looks this:

        (int row = 1; row <= 10; row++) {         (int star = 10; star >= 1; star--) {             if (star >= row) {                 system.out.print("*");             } else {                 system.out.print(" ");             }         }         system.out.println();     } 

the output looks this:

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

i can't seem figure out make whitespace go before stars. i've tried switching loop conditions, gives me same result. there's these for-loops i'm not getting. can point me in right direction :)

so tried analyze code , found is

your mistake:

here see desired output , output becomes different output line number 2 , reason found if condition star >= row lets iterate loop of star row value 2:

if(star >= row) //when star = 10 - condition true. * output  if(star >= row) //when star = 9 - condition true. * output  if(star >= row) //when star = 8 - condition true. * output 

so * output untill star>=row returns false star = 1 scenario iteration.
row = 3 condition true unless star value becomes <=2. problem printing * in start , condition comes after printing *.

possible solution:

basically need print in start, not in end. same condition may need reverse iteration method columns in order reverse print order. if change order of loop can job. lets iterate loop row value of 2:

if(star >= row) //when star = 1 - condition false. ` ` output  if(star >= row) //when star = 2 - condition true. * output  if(star >= row) //when star = 8 - condition true. * output 

so in case printed first , * printed later.

updated code:

i have updated code. have @ inner loop.

for (int row = 1; row <= 10; row++) {     (int star = 1; star <= 10; star++)     {         if (star >= row)         {             system.out.print("*");         }         else         {             system.out.print(" ");         }     }     system.out.println(); } 

hope helps :)


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -