-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapes.java
More file actions
57 lines (47 loc) · 1.38 KB
/
Shapes.java
File metadata and controls
57 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
public class Shapes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int userInput = 0;
boolean invalidnumber = true;
// variable s is just for user to press any key to continue
Scanner s = new Scanner(System.in);
// input reads in user input
Scanner input = new Scanner(System.in);
// welcome message
System.out.println("The program will print out shapres using numbers. ");
System.out.println();
System.out.println("Press enter to continue...");
s.nextLine();
System.out.print("Enter a number between 5 and 15: ");
//input validation
do {
userInput = input.nextInt();
if (userInput >= 5 && userInput <= 15) {
invalidnumber = false;
} else {
System.out.println("Sorry, " + " is not a valid number");
}
} while (invalidnumber);
for (int i = 1; i <= userInput; i++) {
for (int j = 1; j <= userInput; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
// for loop to print out shape
for (int i = 1; i <= userInput; i++) {
for (int j = 1; j <= (userInput - i)*2; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
System.out.print(" " + k);
}
for (int k = 2; k <= i; k++) {
System.out.print(" " + k);
}
System.out.println();
}
}
}