Results 1 to 4 of 4
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default 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.

  2. #2
    Aeroren is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    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();
    }
    EDIT: Note that if all three vehicles are wrecked this will enter a endless loop
    Last edited by Aeroren; 01-06-2011 at 11:32 PM.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    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?\

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    if you wanted to use labels, you'd use C/C++ :p unless you're not talking about programming labels... in which case I'm lost
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Starting Out...
    By maknib in forum Java Gaming
    Replies: 1
    Last Post: 11-11-2010, 08:15 PM
  2. Starting with Databasing
    By DavidG24 in forum JDBC
    Replies: 3
    Last Post: 09-13-2009, 08:51 AM
  3. Replies: 5
    Last Post: 03-20-2009, 10:04 PM
  4. Indexing starting with 1
    By ravian in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 12:03 PM
  5. just starting
    By specbailey in forum New To Java
    Replies: 23
    Last Post: 08-13-2007, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •