Write a program that asks the user to enter an integer between 1 and 15. If the
ID: 3641659 • Letter: W
Question
Write a program that asks the user to enter an integer between 1 and 15. If the number entered is outside that range, your program should print out an error message and re-prompt for another number. You can use whatever input method you want, Scanner or JOptionPane.
Once the user enters a number in the correct range, your program will print out two patterns of asterisks that look like the following. The number provided by the user is equal to the height of the pattern.
Let us say that the input value was 5.
*
**
***
****
*****
*
* *
* *
* *
*****
If the input value is 8, then the output should be:
*
**
***
****
*****
******
*******
********
*
* *
* *
* *
* *
* *
* *
********
As you can see, the input value represents the height of the triangle.
Again, note that the only allowable print statements are of the form indicated in the first problem: System.out.printXX(‘*’) or System.out.printXX(‘ ’), that is, statements that print only a single asterisk or a single space at once.
This does not mean that you are limited to the use of a single printing statement, like System.out.println() for example. You can have any mixture of print(), println(), or printf() statements that you wish. Just make sure that each printing statement, whatever it is, only prints out a single character at once
Bonus Points
You can get an additional 10 bonus points if, in addition to the preceding figures, your program also implements the following patterns. Let us say that h=4:
*
* * *
* * * * *
* * * * * * *
*
* *
* *
* * * * * * * * *
Explanation / Answer
import java.util.Scanner;
public class AsteriskTriangle {
public static void main(String[] args) {
//LOCAL DECLARATIONS
Scanner keyboard = new Scanner(System.in);
int height;
System.out.print("Enter the triangle's height: ");
height = keyboard.nextInt();
while (height < 1 || height > 15) {
System.out.println("The height must be an integer between 1 and 15");
System.out.print("Enter the triangle's height: ");
height = keyboard.nextInt();
}
//Line spacing
System.out.println();
/** Pattern 1
*
* *
* **
* ***
* ****
* *****
*
* Range of line index (line) is [1, height]
* Number (#) of indent spaces per line is (height - line)
* # of asterisks per line is (line)
*/
for (int line = 1; line <= height; line++) {
for (int space = 0; space < height - line; space++)
System.out.print(' ');
for (int asterisk = 0; asterisk < line; asterisk++)
System.out.print('*');
System.out.println();
}
//Line spacing between pattern 1 and pattern 2
System.out.println();
/** Pattern 2
*
* *
* **
* * *
* * *
* *****
*
* Range of line index (line) is [1, height]
* # of indent spaces of each line is (height - line)
* 1st line has 1 asterisk
* Last line has (height) asterisks
* Other lines (body lines) has 2 asterisks, 1 before
* the inner spaces and the other at the end of line
* The inner spaces of each line is (line - 2)
*/
for (int line = 1; line <= height; line++) {
for (int space = 0; space < height - line; space++)//indent spaces
System.out.print(' ');
if (line == 1) { //1st line
System.out.print('*');
System.out.println();
}
else if (line == height) { //last line
for (int asterisk = 0; asterisk < height; asterisk++)
System.out.print('*');
System.out.println();
}
else { //body lines
System.out.print('*');
for (int space = 0; space < line - 2; space++)//inner spaces
System.out.print(' ');
System.out.print('*');
System.out.println();
}
}
//Line spacing between pattern 2 and pattern 3
System.out.println();
/** Pattern 3
*
* *
* ***
* *****
* *******
*
* Range of line index (line) is [1, height]
* Number (#) of indent spaces per line is (height - line)
* # of asterisks per line is (line * 2 - 1)
*/
for (int line = 1; line <= height; line++) {
for (int space = 0; space < height - line; space++)
System.out.print(' ');
for (int asterisk = 0; asterisk < 2 * line - 1; asterisk++)
System.out.print('*');
System.out.println();
}
//Line spacing between pattern 3 and pattern 4
System.out.println();
/** Pattern 4
*
* *
* * *
* * *
* *******
*
* Range of line index (line) is [1, height]
* Number (#) of indent spaces per line is (height - line)
* 1st line has 1 asterisk
* Last line has (height * 2 - 1) asterisks
* Other lines (body lines) has 2 asterisks, 1 before
* the inner spaces and the other at the end of line
* The inner spaces of each line is (2 * line - 3)
*/
for (int line = 1; line <= height; line++) {
for (int space = 0; space < height - line; space++)//indent spaces
System.out.print(' ');
if (line == 1) { //1st line
System.out.print('*');
System.out.println();
}
else if (line == height) { //last line
for (int asterisk = 0; asterisk < height * 2 - 1; asterisk++)
System.out.print('*');
System.out.println();
}
else { //body lines
System.out.print('*');
for (int space = 0; space < 2*line - 3; space++)//inner spaces
System.out.print(' ');
System.out.print('*');
System.out.println();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.