Reputation: 1481
I'm having some issues with while loops. I've got a task to print a diamond with 2 input variables: 1) Integer to represent number of rows/max number of chars in middle 2) Char to be printed: Psuedo code: 7 = int $ = char Program should print (and account for spaces on the left side): http://pastebin.com/cspgz3bA
My while loops are a mess it seems. Right now they are printing (* simulate spaces): http://pastebin.com/cspgz3bA
I've discovered that separating the diamond into two triangles (upper & lower) to be the easiest way of tackling this, however my code seems to be complete garbage...
EDIT:: This is for the top half of the triangle only, hence the (rows/2)+1 - it's meant to stop after finishing the middle row.
rows = integerInput;
maxSpace = integerInput;
while (currentRow <=((rows/2)+1)) {
spaceReq = ((maxSpace -1)/2); // determines spaces required
while (spaces < spaceReq) {
System.out.print("*");
spaces++;
}
while (charPrinted < charReq){
System.out.print(charInput);
charPrinted++;
}
currentRow++;
maxSpace--;
charReq = charReq +2;
System.out.println("");
}
Can someone point out why this works for the 1st iteration, but breaks on the next ones?
Thank you!
Upvotes: 0
Views: 1942
Reputation: 3155
charPrinted
and spaces
need to be reset to 0
at the top of the outer loop.
Upvotes: 3