Incorrect output when printing diamond using do while loop
I am creating code to print a diamond using only Do while loops and I have got the following code:
Code:
public static void diamond3() {
System.out.println("Output for: Do while Loop");
int noOfRows = DIAMOND_SIZE;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Initializing row with 1
int row = 1;
int i = midRow;
do {
//Printing i spaces at the beginning of each row
int j = 1;
do {
System.out.print(" ");
j++;
}
while (j <= i);
//Printing j *'s at the end of each row
j = 1;
do {
System.out.print("* ");
j++;
}
while (j <= row);
System.out.println();
//Incrementing the row
row++;
i--;
}
while (i > 0);
i = 0;
do {
//Printing i spaces at the beginning of each row
int j = 1;
do {
System.out.print(" ");
j++;
}
while (j <= i);
//Printing j *'s at the end of each row
int mid = (row+1)/2;
j = row;
do {
if(i==0 && j==mid)
System.out.print("o ");
else
System.out.print("* ");
j--;
}
while (j > 0);
System.out.println();
//Decrementing the row
row--;
i++;
}
while(i <= midRow);
}
Im meant to get a diamond shape but I am getting the following shape:
The expected out is:
Code:
*
* * *
* * o * *
* * *
*
Can someone help figure out where I have gone wrong. I have checked the code so many times but unable to figure out where the mistake is.
Re: Incorrect output when printing diamond using do while loop
The expected outcome is meant to be a diamond like
*
* * *
* * o * *
* * *
*
Re: Incorrect output when printing diamond using do while loop
Code:
*
* * *
* * o * *
* * *
*
Re: Incorrect output when printing diamond using do while loop
Can you describe what is wrong with the output? Where in the code is the line with the bad content printed?
Also posted at: http://www.dreamincode.net/forums/to...and-o-in-java/