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:
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 *******"); } } } }
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With