• Post Reply Bookmark Topic Watch Topic
  • New Topic

making a method run a certain amount of times  RSS feed

 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


im trying to make an array list with some numbers, i want to use the numbers in the arraylist to tell a method how many times to run. with code it will make sense.





i know the thing i did with turn() * length is incorrect, obviously. what i am trying to figure out is how to do something similar to this. if anyone has any ideas they are greatly appreciated.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you learned about for or while loops yet?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Control Flow Statements in Sun's Java tutorials, where loops are explained.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright, sorry for not giving 100% detail. sometimes it is difficult to format a question to make sense.

anyways...

im writing a class that makes the bug "dance." i have to make a constructor that has an integer array as a parameter. the integer entries represent how many times the bug turns before it moves. when a dancing bug acts it should turn the number of times given by the current array entry. each move should carry out the next number in the array. after carrying out the last number in the array the bug should start over again, so that it continually repeats the same "dance" over and over.

overall the main thing is that, the dancingbugrunner should create an array and pass it as a parameter to the dancingbug constructor.

control statements arent really my problem. my problem now is that i am trying to get my bug to dance. i have gotten it to turn, but i cant get it to turn the amount of times that it is supposed to, move, and then turn the next number etc.



if anyone knows anything that would help it would be appreciated.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you pass an array in somewhere with a bunch of numbers. for each element/number in the array, you need to have the bug turn that many times.

is this correct?

if so, I'd write a method that takes an integer, and turns the bug that many times.



Then for each element in the array, call the method above and pass in the current element.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i have gotten this far. i can make it take the number and turn. what i am trying to achieve though is for the bug to turn the number of times that it needs, move and repeat. i have the turn method in the right place, but im having trouble figuring out where the move method fits in.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried placing the move method in lots of places, but i always get strange output. sometimes the bug just jumping across the screen.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nathan gibson wrote:i have tried placing the move method in lots of places, but i always get strange output. sometimes the bug just jumping across the screen.


At a high level, it sould seem to me that you should call the method to move your bug after the method that executes all the turns is complete.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried something of that order on my second post with code. i got very strange results with that. i have tried most of the obvious. the output with that code was something like the bug would make like a half circle and stop...

 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nathan gibson wrote:i tried something of that order on my second post with code. i got very strange results with that. i have tried most of the obvious. the output with that code was something like the bug would make like a half circle and stop...





Ok, so you want the bug to turn a certain number of times, then move, then turn again? Looking at the logic here, you call turn();, then go into a for loop that moves the bug, then back to the outer for loop to turn again, then move again, etc. What is the code in turn(); and move();? I didn't see it in your earlier posts, or if it is there I have been known to miss things directly in front of my face.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im kind of on that track. im working with a nested loop.


i dont know if it helps, but with this full code:


my output is attached. its like not making since to me at this point. i know that the for loop inside has a weird counter, but i wasnt exactly sure what i would use. if anyone has advice it would be very much appreciated


Untitled.jpg
[Thumbnail for Untitled.jpg]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!