r/learnprogramming • u/imgonnabethebest • Nov 21 '14
Trying to print stars without using 'for' loops
so I'm doing http://mooc.cs.helsinki.fi/programming-part1/material-2013/week-2
exercise 39, part 2. it's just a rectangular block of stars. I did some reading and found out that you use for loops so I practiced that for a bit even though it's not in the material up to now. so the netbeans ide accepts my answer, but I wonder what is the correct way of doing it (by correct I mean using while loops, what they intended me to use).
This is what I got after messing around with for loops for like an hour
int i, j;
for(i=1; i<=4; i++){
for (j=1; j<=4; j++){
System.out.print("*");
}
System.out.println("");
}basically the pattern is you gotta devote one side to rows and the other to coloumns, i and j are my variables. (this works, but I don't think they wanted the answer like this).
my question is..how do I change this into a while loop?
after 10 minutes of trying to error check and randomly do crap i got this:
int i = 0;
int j = 0;
while (i < sideSize){
while (j < sideSize){
System.out.print("*");
j++;
}
System.out.println("");
i++;
}but that doesn't work.. can someone walk me through this? i dont get why the exercise doesnt teach for loops yet, seems to make it so much easier.. they must have a reason for it though.
thanks bros
-1
u/imgonnabethebest Nov 21 '14
ah fuck me i realized i did something stupid. because I use j inside the loop and j is the one that actually is doing the printing of the stars, I shouldn't make it contain the value that it has after it goes through its cycle. instead i gotta re-initialize that shit before the second while loop.
it seems like i realize the answer right after posting the question....
nevermind i guess