0

My code looks like this:

public class HourGlass{
public static final int x = 5;
   public static void wall(){
   System.out.println("|\"\"\"\"\"\"\"\"\"|");
   }
   public static void top(){
   for (int i = 1;i<=x;i++){
     for (int j =1; j<=i; j++){
     System.out.print(" ");
     }
     System.out.print("\\");
     for (int k = 1; k <= 9-2*i ;k++){
     System.out.print(":");
     }
     System.out.print("/\n");
   }
}

public static void bottom(){
  for (int i = 1;i <= x; i++){
     for (int j = 1; j<= 5-i; j++){
        System.out.print(" ");
        }
     System.out.print("/");
     for (int k = 1; k <= 2*i-1; k++){
     System.out.print(":");
     }
     System.out.print("\\\n");
     }
}

 public static void main(String[] args){
wall();
top();
bottom();
wall();
}
}

And I get this:

|"""""""""|
 \:::::::/
  \:::::/
   \:::/
    \:/
     \/
    /:\
   /:::\
  /:::::\
 /:::::::\
/:::::::::\
|"""""""""|

How would I go about fixing this and making the bottom and top scale? I have tried to figure out how to solve this issue but I am rather stuck and have not found anything.

viveknaskar
  • 2,136
  • 1
  • 20
  • 35
Origami
  • 21
  • 3
  • 1
    The same way you print a sequence of `:` whose length depends on a variable you can do that with the `"` of the walls. – Michael Butscher Oct 09 '18 at 01:18
  • 1
    Side note: the upper side of the hourglass isn't rendered correctly yet. There should be 2 `:`s near the center of the hourglass. – Jai Oct 09 '18 at 01:22

2 Answers2

1

This code could be made less rigid, however, this was not your question. In order to make the top and bottom match, you need to ignore the top row where colons do not appear.

I changed your x variable to a capital X, because it is a constant, and made a couple tweeks to your top() and wall() code.

public static final int X = 5;

public static void wall() {
    System.out.println("|\"\"\"\"\"\"\"\"\"\"\"|");
}

public static void top() {
    for (int i = 1; i <= X + 1; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(" ");
        }
        System.out.print("\\");
        for (int k = 1; k <= 9 - 2 * (i - 1); k++) {
            System.out.print(":");
        }
        System.out.print("/\n");
    }
}

Hope this helps

SynchroDynamic
  • 386
  • 2
  • 14
1

I always find these exercises to be easier to code if there is a method for creating a string with a repeated character. There are other ways to write it, but here is one way to do this with minimal code, using a recursive method:

private static String repeat(char c, int count) {
    return (count == 0 ? "" : c + repeat(c, count - 1));
}

E.g. if you call repeat('*', 5) it returns *****

With that available, you can easily print an hourglass. I was a bit unsure what the center of the hourglass should be, so here is two methods.


Hourglass with even number of colons

public static void printHourGlassEven(int size) {
    System.out.println('|' + repeat('"', size * 2) + '|');
    for (int i = 1; i <= size; i++)
        System.out.println(repeat(' ', i) + '\\' + repeat(':', (size - i) * 2) + '/');
    for (int i = size; i >= 1; i--)
        System.out.println(repeat(' ', i) + '/' + repeat(':', (size - i) * 2) + '\\');
    System.out.println('|' + repeat('"', size * 2) + '|');
}

Test

printHourGlassEven(5);

Output

|""""""""""|
 \::::::::/
  \::::::/
   \::::/
    \::/
     \/
     /\
    /::\
   /::::\
  /::::::\
 /::::::::\
|""""""""""|

Hourglass with odd number of colons

public static void printHourGlassOdd(int size) {
    System.out.println('|' + repeat('"', size * 2 - 1) + '|');
    for (int i = 1; i < size; i++)
        System.out.println(repeat(' ', i) + '\\' + repeat(':', (size - i) * 2 - 1) + '/');
    System.out.println(repeat(' ', size) + 'X');
    for (int i = size - 1; i >= 1; i--)
        System.out.println(repeat(' ', i) + '/' + repeat(':', (size - i) * 2 - 1) + '\\');
    System.out.println('|' + repeat('"', size * 2 - 1) + '|');
}

Test

printHourGlassOdd(5);

Output

|"""""""""|
 \:::::::/
  \:::::/
   \:::/
    \:/
     X
    /:\
   /:::\
  /:::::\
 /:::::::\
|"""""""""|
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    This is what I was talking about in my answer. This is far less rigid. This implementation allows one to make a perfect hour glass of any size. – SynchroDynamic Oct 09 '18 at 14:17