Can someone help me to create a small program in which I can create star designs
ID: 3623770 • 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
please rate - thanks
import java.util.*;
public class stars
{public static void main(String [] args)
{Scanner in=new Scanner(System.in);
int i,j,rows;
char type;
for(; ;)
{
type=menu(in);
if(type=='X')
System.exit(1) ;
System.out.print("How many rows? ");
rows=in.nextInt();
switch(type)
{case 'A':A(rows);
break;
case 'B':B(rows);
break;
case 'C':C(rows);
break;
case 'D':D(rows);
break;
default: System.out.println("Invalid type");
}
}
}
public static char menu(Scanner in)
{char t;
String input;
System.out.println("What type of star");
System.out.print("Enter A,B,C, D or X to exit: ");
input= in.next();
t=input.charAt(0);
return t;
}
public static void A(int rows)
{int i,j;
for(i=0;i<rows;i++)
{for(j=0;j<=i;j++)
System.out.print("*");
System.out.println();
}
}
public static void B(int rows)
{int i,j;
for(i=rows;i>0;i--)
{for(j=0;j<i;j++)
System.out.print("*");
System.out.println();
}
}
public static void C(int rows)
{int i,j;
for(i=rows;i>0;i--)
{for(j=0;j<rows-i;j++)
System.out.print(" ");
{for(j=0;j<i;j++)
System.out.print("*");
System.out.println();
}
}
}
public static void D(int rows)
{int i,j;
for(i=0;i<rows;i++)
{for(j=0;j<rows-i-1;j++)
System.out.print(" ");
{for(j=0;j<=i;j++)
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.