Can someone help me to create a small program in which I can create star designs
ID: 3623764 • Letter: C
Question
Can someone help me to create a small program in which I can create star designs according to the user input. This have to be done with loops because it has to show that if the user first hits A , and then he inputs 3 for the number of rows then the design is:
*
**
***
if the design inputs B and then, lets say 4 for the number of rows the design will be:
****
***
**
*
If the user inputs c and then, any number (lets say 5) for the numbers of rows the output is.
*****
****
***
**
*
Finally in the case that the user inputs d and then lets say 4 the output is:
*
**
***
****
If the user inputs x then the program will end the loop
Explanation / Answer
In your post, the (b) star shape looks exactly like the (d) shape and the (a) shape looks exactly like the (c) shape. I assumed that what happened was that some indentation was left out so that the stars in (c) and (d) were supposed to be right-justified. Here is my code based on that assumption... you'll have to alter it if (c) and (d) were supposed to do something different.
import java.util.*;
public class StarDesign {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("Which type of star? (enter a, b, or c, or x to quit): ");
char starType = in.nextLine().charAt(0);
if (starType == 'x') break;
System.out.print("How many rows? ");
int rows = in.nextInt();
in.nextLine();
System.out.println();
if (starType == 'a') {
for (int i=1; i <= rows; i++) {
// print out i number of stars
for (int j=0; j<i; j++) {
System.out.print("*");
}
System.out.println();
}
}
if (starType == 'b') {
for (int i=rows; i > 0; i--) {
// print out i number of stars
for (int j=0; j<i; j++) {
System.out.print("*");
}
System.out.println();
}
}
if (starType == 'c') {
for (int i=1; i <= rows; i++) {
String rowString = "";
// String together i number of stars
for (int j=0; j<i; j++) {
rowString = rowString+"*";
}
for (int j=i; j<rows; j++) {
// Add the indentation before the stars
rowString = " "+rowString;
}
System.out.println(rowString);
}
}
if (starType == 'd') {
for (int i = rows; i > 0; i--) {
String rowString = "";
// String together i number of stars
for (int j=0; j<i; j++) {
rowString = rowString+"*";
}
for (int j=i; j<rows; j++) {
// Add indentation before the stars
rowString = " "+rowString;
}
System.out.println(rowString);
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.