java - Increase of 3 variables -


in program, have 3 variables: when 1 of these variables reaches 100, must appear words "variable came first finish line."

how organize arrival of second , third variable, come out this:

variable1-arrived first variable2-finished second variable3 finished third 

help!

public corsa(string name) {     this.name = name;     system.out.println("start: " + name);      system.out.println("---------------"); }  public void run() {     while(finita == false)     {         try         {             avanza = (int) (math.random()*20+1);             percorso = percorso + avanza;             system.out.println(name + " has path " + percorso + " meters");             if(percorso < 100)             {                 system.out.println("---------------");                 sleep = (int) (math.random()*20+1);                 thread.sleep(sleep);             }             else             {                 system.out.println("---------------");                 system.out.println("---------------");                 system.out.println(name + " came in first");                  finita = true;             }         }         catch(interruptedexception e){}         thread.yield();     } } 

}

i haven't tested (so might not compile), following should work:

public class myrace {     private int distance = 100;     private float offset = 20;     public int runners[3];      public void run()     {         // set runners 0         ( int = 0; < runners.length; i++ )             runners[i] = 0;          // run race , stop when @ least 1 runner has reached distance...         boolean finished = false;         while ( !finished )         {             ( int = 0; < runners.length; i++ )             {                 runners[i] += (int)((math.random() * offset) + 1);                 if ( runners[i] >= distance ) finished = true;             }         }         // race finished sort runners         treemap<string, int> ranking = new treemap<string, int>();         ( int = 0; < runners.length; i++ )         {             // treemap sorted on key, not value!             // runners number tagged on, in case 2 runners have finished on same distance.             string sortedkey = integer.tostring(runners[i]) + "." + integer.tostring(i);             ranking.put(sortedkey, i);         }         // print results         int pos = 1;         ( map.entry entry : ranking.entryset() )         {             string key = entry.getkey();             string distance = key.substring(0, key.indexof(".")); // chop off "." + runners number.              system.out.println("#" + pos +    // position                 "." + entry.getvalue() +      //                 ", distance = " + distance);  // distance covered              pos++; // take in account whether multiple runners finished on same distance.         }     } } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -