-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyramid.java
More file actions
40 lines (36 loc) · 851 Bytes
/
pyramid.java
File metadata and controls
40 lines (36 loc) · 851 Bytes
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
import java.io.Console;
class Pyramid {
public static void main(String [] args){
Console console = System.console();
String userInput = console.readLine("Please enter how tall you want the pyramid: ");
int userNumber = Integer.parseInt(userInput);
int x = 1;
int y = 1;
int lastLine = lastLineSize(userNumber);
while (x <= userNumber) {
int spaces = (lastLine - y)/2;
printSpace(spaces);
printX(y);
y = y + 2;
x++;
System.out.print("\n");
}
}
private static int lastLineSize(int x){
return x + (x-1);
}
private static void printX(int times){
int x = 0;
while (x < times){
System.out.print("*");
x++;
}
}
private static void printSpace(int times){
int x = 0;
while (x < times){
System.out.print(" ");
x++;
}
}
}