• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops and nested if else statements  RSS feed

 
Linda Selfridge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am in a pickle at the moment and am really not sure where to start on this problem, so i am looking for guidance and a little help really.

I have to design a program that plots points within a square. The user basically has to enter a command at a prompt such as: up, down, left or right.
The program should then move 1 point either positive or negative from the original point 0,0.

Now I have to use a loop so that the program repeatedly prompts for a new move until such time as the position of the point hits a boundary. But i also have to include some type of nest if else statement.

I am so new at this that my brain refuses to even contemplate it.

Where do I start?

Please help! I have been trying and reading up on this for days and am still floundering!

Thank you!
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break up your problem into smaller steps:

1. Look at the Scanner class. That class will give you a way to accept input from the user (i.e. up, down, right, left).

2. Think of a way to represent your square. It sounds like a 2-dimensional array of integer would work. It would hold the x,y coordinates of your square.

3. Consider using a while loop. You would loop until the user hits the boundary. Loops can have a block of statements to execute (indicated by brackets { } ). You will need an if-then-else statement inside this loop block to check if the user has hit a boundary or not.

4. Think about how to determine if the user hits a boundary. You could do this with two int variables (e.g. int currentX, currentY). As the user enters move commands, you could increment/decrement the currentX and currentY variables accordingly. You would then check this against the boundaries of the square.

Start small and look at the individual tasks above. Once you're comfortable with them, you can put them all together.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't start by thinking "I need a for loop inside an if loop..".

Start by writing down things you need to do in VERY general terms:

1) Get input from user (prompts user for input)
2) move cursor (takes a direction as an input parameter)
3) check to see if boundary hit (returns true or false)

there may be a few other things, but these are the highlights. I would start by writing methods that do these various things. You will probably write a lot of code that won't be needed in your final program, but that's OK. For example, I would write a method that moves the cursor, and then write a bunch of code to call that method with hard-coded values of "up", "down", etc. Maybe even a loop or two that call it a bunch of times.

once each of these method works, start wiring them together. Write new code that calls the "getInput()" method, saves the value, then passes that value to the "moveCursor()" method.

Once that works, work in your code to check the boundary. Just have it (at first) print "I smacked into the wall" or something - an return an appropriate boolean. But I would ignore what it returns at first.

once all THAT works, then put in code to check after each move if you hit the wall, and quit if you do...

The trick is to build your code a small, testable piece at a time. recompile and test OFTEN - after writing every 2-3 lines at most. throw in lots of System.out.println() statements to see what your code is doing. TEST TEST TEST TEST.



 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really need an if statement here. If the program is required to repeatedly prompt for new move until it hits a boundary you only need one while loop that checks each time the value of x and y and compares them to the boundary extremes
>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!