In: Computer Science
write a program using the main method where the user enters a number greater than 0, and the program prints out a set of stairs. The stairs consist of 3 boxes stacked on top of each other where the length and width of the first box is the number, the length and width of the second box is the number plus 1, and the length and width of the third box is the number plus 2. The stairs should be drawn using asterisks.
Solution Tests:
**
**
***
***
***
****
****
****
****
***
***
***
****
****
****
****
*****
*****
*****
*****
*****
****
****
****
****
*****
*****
*****
*****
*****
******
******
******
******
******
******
PROVIDED CODE:
* Provided code. Do not alter the code that says "Do not alter" * Make sure this at least compiles (no syntax errors) * You may write additional methods to help */ //Do not alter----------------------------------------------------------------------- import java.util.Scanner; public class Question01 { public static void main(String[] args) { int number;//Used for the stairs if(args == null || args.length == 0) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the value to draw some stairs"); number = keyboard.nextInt(); keyboard.nextLine(); } else { number = Integer.parseInt(args[0]); } //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //Space for other methods if necessary----------------------------------------------- //Write those here if necessary //----------------------------------------------------------------------------------- }//Do not alter this /*Solution Description * */
Source Code:
Output:
Code in text format (See above images of code for indentation):
import java.util.Scanner;
/*class definition*/
public class Question01
{
/*main method*/
public static void main(String[] args)
{
int number;/*Used for the stairs*/
if(args==null||args.length==0)
{
/*read value from user*/
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the value to draw some stairs");
number=keyboard.nextInt();
keyboard.nextLine();
}
else
{
/*convert to integer*/
number=Integer.parseInt(args[0]);
}
/*using nested loops print 3 boxes*/
/*outer loop iterates for 3 times for 3 boxes*/
for(int i=0;i<3;i++)
{
/*below two loops prints a box of length and width*/
for(int j=0;j<number;j++)
{
for(int k=0;k<number;k++)
System.out.print("*");
/*new line*/
System.out.println();
}
/*increment number for next iteration*/
number+=1;
}
}
}