Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created April 30, 2020 20:07
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 mariiaKolokolova/188504102f816153d5ab304ad19d1e24 to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/188504102f816153d5ab304ad19d1e24 to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Input hourglass width:");
int w;
int ch;
w = sc.nextInt();
ch = w % 2;
if (ch != 0) {
int midl;
midl = w / 2 + 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
if (i <= midl) {
if (j <= i - 1 || j > w - i + 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
} else {
if (j < w - i + 1 || j > i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
}
System.out.println("");
}
} else {
System.out.println("Your number is not correct. Please try one more time.");
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment