• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java loop

Joined
Nov 26, 2006
Messages
620
Ok so for a project thats due friday I have to create a program that counts steps and floors as if you were climbing them. There are 10 steps between each floor and 5 floors and then the program must count down from floor 5 step 0 back to floor 0 step 0. so the output should look like this.

you are on floor 0 step 0
you are on floor 0 step 1
......
you are on floor 4 step 10
you are on floor 5 step 0
you are on floor 4 step 10
you are on floor 4 step 9

I really dont even know where to start. heres what ive got so far but im not sure where to go with it.
Code:
public class Project4Loops {
    public static void main(String[] args) {
        int floor = 0;
        int step = 1;
        while (floor <5 && step <11) {
            System.out.println("You are on floor " + floor + " step " + step);
            step++;
        }
    }
    
}
 
Well, firstly you're going in the wrong direction (at least according to your description)

Second, you have 2 iterators, so you're going to have 2 loops.
(floors, and steps)

I'll try to help without doing your homework for you... so start with the above hints and see what you can come up with.
 
Perhaps the first thing you should figure out is how to climb one floor, counting each step on the way. Once you can climb one floor, treat that block of code as a single "piece". Now, how would you climb multiple floors? Or, what if each floor only had one step? Then how would you climb multiple floors. Combine these concepts together and you should be able to figure out how to climb multiple floors with multiple stairs.

I'm sure you can figure out how to print out:

1
2
3
4
5

How much harder is it to print:

1:1
1:2
1:3
1:4
1:5
2:1
2:2
2:3
2:4
2:5
 
btw i was wrong about how the output should look. Its gotta go like this

you are on floor 0 step 0
you are on floor 0 step 1
......
you are on floor 4 step 10
you are on the roof
you are on floor 4 step 10
you are on floor 4 step 9

guess theres not really much of a difference. Anyway ill post some updated code soon so you can see where im at.
 
The following is what I have but its not much progress. I don't know how to increment the floor variable for every tenth increment in the step variable. An I dont know how to reset the step variable to 0 after every increment in the floor variable.
 
Ok well first think of it like this:

You are on a floor.
Can we count the next floor?
No we have to count all 10 steps first.
Count step 1.
Can we count the next floor?
No we have to count all 10 steps first.
Count step 2.
Can we count the next floor?
etc.

So basically what you are going to want to do, is have your loop for counting floors and then a SECOND loop inside of that one for counting steps. You cannot increment to the next floor without first going through the entire loop for floors.

Edit: You are not going to get the answer on this forum, but if you have questions feel free to PM me and I can shoot you my AIM and we can work on this together.
 
ditto. follow the hints vage has given and you should be able to knock it out.
 
My first question would be regarding the intended result. You mentioned each floor has 10 steps, but the output includes step 0 as well as step 10. If you count from 0 to 10 you will end up with 11 steps. Sometimes one of the hardest issues of programming is relizing that unless otherwise initialized, everything starts at zero.

Have you covered the 'remainder' modifier yet? If so, you should be able to produce the desired result with just one loop and and if-else statement.

What about recursive functions? This would be the most elegant way of writing this particular problem. I won't get into it much if you haven't covered those yet, as they could get quite complicated.
 
ok this is what ive got so far... It counts up the steps and floors up to the roof floor (though I need it to say roof only once ill have to iron that out). Only thing is im not sure how to get it to reverse.
Code:
public class Project4Loops {
    public static void main(String[] args) {
        int floor = 0;
        int step = 0;
        for (floor = 0; floor < 6; floor++) {
            for (step=1; step <11; step++) {
                if( floor == 5)
                    System.out.println("You are on the roof");
                else
                    System.out.println("You are on floor " + floor + " step " + step);
            }
        }
    }
}
 
ok this is what ive got so far... It counts up the steps and floors up to the roof floor (though I need it to say roof only once ill have to iron that out). Only thing is im not sure how to get it to reverse.
Code:
public class Project4Loops {
    public static void main(String[] args) {
        int floor = 0;
        int step = 0;
        for (floor = 0; floor < 6; floor++) {
            for (step=1; step <11; step++) {
                if( floor == 5)
                    System.out.println("You are on the roof");
                else
                    System.out.println("You are on floor " + floor + " step " + step);
            }
        }
    }
}


Now you're on the right track here.


Something to think about (to reverse the loops) is to use an "incrementing" value.
To go forward, increment by 1. to go back, increment by -1.

Of course, you'd have to modify your loops, as well as put in some "stopping" condition logic (otherwise you'd go up, come down, go back up, and come back down endlessly... unless that is a desired result)
 
After what you got so far (ish, I didn' tlook close enough to make sure), just do the loop in reverse. That should net you going up then going back down.
 
You're on the right track as far as the algorithm is concerned but I'm not sure if you're correctly addressing the problem. You say there are 5 floors and 10 steps between each floor yet in your code you're going to end up with 6 floors and start at step 1 when your sample output shows that you start at step 0. You're on the right track, just need to iron out these minor details.
 
Back
Top