java - Performing loops for first time? Really simple? -


i have assignment loops need on. quite simple don't know how use loops. need print out 3 shapes using loops. shapes made of asterisks, i'm allowed print 1 asterisk @ time, must loops print shapes. first shape 10 asterisks in row 1 after another. second 1 ten lines 1 asterisk on each line. last 1 hardest one. shape has ten lines. , first line has 1 asterisk, second line has two, third 1 has 3 on , forth, tenth line has ten asterisks. tried doing past 3 hours using previous assignment isn't working out. here is:

public class assignment9 {     public static void main(string args[]) {         (integer = *; >= *; i=i-*) {             if ((i % *) == *) {                 system.out.println("i = " + i);             }         }     } } 

i'm taking computer science class, science credit need. don't plan on being computer scientist. can see, not forte.

here's how can reason third shape. should able figure out other two, after this, since simpler:

you need print 10 lines. way count 0 , move 10 (excluding 10, otherwise you'd 11 iterations). move towards target increasing counter 1 (if i counter, i++ increases count one). so, @ first variable has value of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. here's part:

for(int = 0; < 10; i++) {         // means: "repeat 10 times following" 

the first line printed when i 0 , need print 1 asterisk that. second line printed when i 1 , need print 2 asterisks that. , on. so, in general, need print many asterisks whatever value of counter is, plus 1 (your counter i, need (i + 1) asterisks). since may print 1 asterisk @ time, can repeat (i + 1) times printing of single asterisk, this:

    for(int k = 0; k < (i + 1); k++)   // means "repeat (i + 1) times...         system.out.print("*");         // ...the printing of single asterisk" 

once you've printed required asterisks line, move next line:

    system.out.println();              // means "move next line" } 

and it's on already.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -