I'm taking a course and I'm in about my 3rd week, totally new to this otherwise.
The assignment was as follows: Including a do-while statement, create a program that asks for user input for an integer, then prints a diamond to the console based on that integer. For example, if the integer is 5, it should be 5 lines into the middle.
* * * * * * * * * * * * * * * *
Without further delay, here is what I've come up with for the first half of the diamond only. I'm aware it's so far from optimal that it's probably hilarious, but I've been struggling with this for a long time now and want to finish this if it can work.
The comments should explain my methodology--and lastly, I greatly appreciate any guidance.
Oh, I should say my result looks like this... the spaces aren't happening at the beginning.
* * * * * * * * *
import javax.swing.JOptionPane; public class doWhile { public static void main(String[] args) { String symbol = "*", spacer = " ", gap = " "; String uinput = JOptionPane.showInputDialog("Enter an integer."); int size = Integer.parseInt(uinput); int spaceCount = size; int gapCount = 1; // This first 'for' statement places the first asterisk. I do this separately because // it makes it easier later on when there is a 'gap' in between each asterisk, which // has to go up by 2 in the loops. for (int a = 1; spaceCount == 0; spaceCount--); { System.out.print(spacer); // Prints a space until the count is 0. } System.out.print(symbol); // Prints the first asterisk in the line. spaceCount = size; // Resets the spaceCount to be the size again. for (int b = 1; spaceCount == 0; spaceCount--); { System.out.println(spacer); // Prints a space until the count is 0. } size--; // Size gets reduced as the 1st diamond line is complete. spaceCount = size; // spaceCount becomes new size. do { // Same methodology occurs for the first half of the diamond. // gapCount is introduced to go up by 2 with each line, as this // is what happens internally in a diamond shape. gapCount = gapCount + 2; for (int c = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases { System.out.print(spacer); // Prints spacer, repeats until spaceCount is 0. } spaceCount = size; // Resets spaceCount to the size. System.out.print(symbol); // Prints 1st asterisk in line. for (int d = 1; gapCount > 1; gapCount--) { gap = gap + (" "); // Gap becomes single space, plus 2 additional spaces. } System.out.print(gap); // Prints the gap between asterisks System.out.print(symbol); // Prints 2nd asterisk in line for (int e = 1; spaceCount == 0; spaceCount--); // Spaces decrease as gap size increases { System.out.println(spacer); // Prints spacer, repeats until spaceCount is 0. } size--; // Size is reduced by one more line. spaceCount = size; // Spacecount becomes new size. } while (size > 0); // Whole process runs until size no longer has a value. System.exit(0); } }