Results 1 to 4 of 4
- 01-06-2011, 11:07 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 11
How to start a for loop back to top without starting the var count over?
Just messing with Polymorphism and this is the main code.
I am within a for loop and if a certain number is picked and the boolean == true. Then go ahead and pick new number. (I know this is inefficient, but I would still like to do this.)
Java Code:import java.util.Random; public class Main { public static void main(String[] args) { Vehicle[] vehicle = new Vehicle[3]; vehicle[0] = new Truck(); vehicle[1] = new Car(); vehicle[2] = new Bike(); Random rand = new Random(); int pick; for (int i = 0; i < 2000; i++) { pick = rand.nextInt(3); if (vehicle[pick].wrecked == true) [COLOR="Red"]// If true I need to pick another rand number [/COLOR] vehicle[pick].drive(); } } }
Last edited by AcousticBruce; 01-06-2011 at 11:23 PM.
- 01-06-2011, 11:30 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
I'm not sure if this is what you want, but this should work...
Java Code:int pick; for (int i = 0; i < 2000; i++) { pick = rand.nextInt(3) while (vehicle[pick].wrecked) { pick = rand.nextInt(3) } vehicle[pick].drive(); }
Last edited by Aeroren; 01-06-2011 at 11:32 PM.
- 01-06-2011, 11:42 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 11
Wow... this should have been obvious... too much programming is causeing me to loose my mind! :eek:
So what if I wanted labels. How can I do that?\
- 01-07-2011, 12:23 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
Similar Threads
-
Starting Out...
By maknib in forum Java GamingReplies: 1Last Post: 11-11-2010, 08:15 PM -
Starting with Databasing
By DavidG24 in forum JDBCReplies: 3Last Post: 09-13-2009, 08:51 AM -
How do you start a Java program from the "Start" menu under Windows?
By ScottVal in forum New To JavaReplies: 5Last Post: 03-20-2009, 10:04 PM -
Indexing starting with 1
By ravian in forum New To JavaReplies: 4Last Post: 01-04-2008, 12:03 PM -
just starting
By specbailey in forum New To JavaReplies: 23Last Post: 08-13-2007, 11:25 PM
Bookmarks