-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject2.java
executable file
·69 lines (53 loc) · 2.57 KB
/
Project2.java
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
58
59
60
61
62
63
64
65
66
67
68
69
/*
Problem:
A yield sign encloses the word YIELD within a triangle.
Write a program that displays a yield sign. (Use stars to represent the sides of the triangle)
*/
package apcs.project2;
public class Project2 {
public static void main(String[] args) {
final int triangleHeight = 20; // Set to change triangle height (To see 'YIELD' text, set to a high number)
final boolean printStars = false; // Set to true if you want the program to print stars instead of slashes / dotted lines
for(int line = 0; line < triangleHeight; line++) { // Runs for each line of the triangle
int leftWhitespace;
for(leftWhitespace = 0; leftWhitespace < line; leftWhitespace++) {
System.out.print(" ");
}
if(line == 0) { // Runs on first line to draw bottom of triangle
for(int i = 0; i < triangleHeight; i++) {
if(printStars) {
System.out.print(" *");
} else {
System.out.print(" _");
}
}
System.out.println();
} else {
if(printStars) {
System.out.print("*");
} else {
System.out.print("\\");
}
int totalTriangleWhitespace = 2 * (triangleHeight - leftWhitespace - 1); // Number of total whitespace to be printed inside of the triangle
for(int middleWhitespace = 0; middleWhitespace < totalTriangleWhitespace; middleWhitespace++) {
final int triangleMiddle = triangleHeight / 2; // Line number of the middle line for the triangle
if(line == triangleMiddle) {
if(middleWhitespace == (totalTriangleWhitespace / 2) - 2) { // If printing in the middle of the middle line minus two characters
System.out.print("YIELD");
middleWhitespace += 4; // Push counter 4 characters forward to make up for the space that 'YIELD' takes up.
} else { // Runs when printing middle whitespace characters on middle line before/after yield
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
if(printStars) {
System.out.println("*");
} else {
System.out.println("/");
}
}
}
}
}