Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't wrap my head around the "draw some stairs with stick-men" program

Tags:

java

for-loop

You've probably seen it before in a Java 1 class: it's a problem that asks you write a program that draws the following figure:

enter image description here

I have to use a constant. I am not allowed to use anything but for-loops, print, and println. No parameters, no arrays. I know how I could do it with parameters and arrays, lucky me. Any help is appreciated!

Here is my incomplete code:

public class Stairs {     public static final int LENGTH=5;      public static void main(String[] args) {         printStairs();     }      public static void printStairs() {         for (int allStairs=1; allStairs<=15; allStairs++) {             for (int spaces=1; spaces<=(-5*allStairs+30); spaces++) {                 System.out.print(" ");             }             for (int stair = 1; stair <= 5; stair++) {                 System.out.println("  o  *******");              }         }     } } 
like image 538
Monica Avatar asked Jul 23 '12 07:07

Monica


1 Answers

This sounds like a homework question, so I don't just want to give you the answer, but try breaking it down into steps. Think about the things that you know:

1) Every stickman has this shape:

  o  ******  /|\ *       / \ *      

2) You can print this out using the following code:

System.out.println("  o  ******"); System.out.println(" /|\ *     "); System.out.println(" / \ *     "); 

3) You can print multiple by using a loop:

for (int stair = 1; stair <= LENGTH; stair++) {     System.out.println("  o  ******");     System.out.println(" /|\ *     ");     System.out.println(" / \ *     "); } 

Think about what kind of output this would give you, and what needs to be changed. Realize that each stickman needs to be indented a certain amount. Figure out how to indent them appropriately based on the value of stair.

like image 197
Jon Newmuis Avatar answered Sep 21 '22 18:09

Jon Newmuis