• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop help please  RSS feed

 
James Leon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am brand new to Java and it is my first programming language, but I'm struggling to understand loops. As part of the assignment I am doing, I have been given some code that I have to edit/add too depending on what the question asks. The question that I am stuck on is:

"Write an exercise() method in the RestrictedHoverFrog class that implements the following behaviour:

– If the second argument to the receiver has the value true, attempt to move the receiver left, then right, then right again, then left again – with this sequence of moves repeating according to the number of times specified by the first argument.

– If the second argument to the receiver has the value false, attempt to move the receiver right, then left, then left again, then right again – with this sequence of moves repeating according to the number of times specified by the first argument."

So far I got the following code. I know it's wrong and that it doesn't repeat the sequence, but I don't know how I can get it to repeat the sequence depending on the number of times specified by the first argument.




Any help will be really appreciated please
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to use a for loop?



The first parameter is your "counter". See if you can figure out how to make it loop.

[edit] This might be clearer if I had said:
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to The Ranch!
 
James Leon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Do you know how to use a for loop?



The first parameter is your "counter". See if you can figure out how to make it loop.

[edit] This might be clearer if I had said:


Thank you so much!! Managed to get it working now
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Leon wrote:
Try to avoid such writing as:
Instead, write:

If you'd ever by mistake manage to write:
You'd get unexpected results by assigning something to true on line 2.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!