Skip to content

Instantly share code, notes, and snippets.

@gdusbabek
Created October 4, 2018 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdusbabek/cbe60ef57bd3f9e0f02c8e590955b10c to your computer and use it in GitHub Desktop.
Save gdusbabek/cbe60ef57bd3f9e0f02c8e590955b10c to your computer and use it in GitHub Desktop.
public class Hourglass {
public static void main(String args[]) {
// top
for (int i = 0; i < 5; i++) {
// print header
if (i == 0) {
System.out.println("|\"\"\"\"\"\"\"\"\"|");
} else {
// print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// print slash
System.out.print("\\");
// print colons 9-2*i
for (int j = 0; j < 9-2*i; j++) {
System.out.print(":");
}
// print slash
System.out.print("/");
// print newline
System.out.println();
}
}
// bottom
for (int i = 5; i > 0; i--) {
// maybe print footer
if (i == 1) {
System.out.println("|\"\"\"\"\"\"\"\"\"|");
} else {
for (int j = 0; j < i-1; j++) {
System.out.print(" ");
}
// print slash
System.out.print("/");
// print colons 11-2*i
for (int j = 0; j < 11-2*i; j++) {
System.out.print(":");
}
// print slash
System.out.print("\\");
// print newline
System.out.println();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment