Assignment description:
You will write a JAVA program that will simulate a horse race with 10 horses. You must use an array. Horse 1 will be in position 1, horse 2 in position 2 and so on. All the horses will begin at location 0 (with a zero value in that position of the array). The user will press enter for all the horses to advance. Every time enter is pressed you will generate a random number between 1 and 3 for each of the ten horses, this will be the amount of spaces that the horse will "move" forward. That number will be added to the previous number that the horse had. The first horse to get to 15 will be the winner. Your program should stop after a horse wins. On each iteration you will display the location of each horse.
//WHEN THE USER HITS ENTER NUMBERS ARE ADDED TO PREVIOUSLY GENERATED NUMBERS
//FIRST HORSE TO FIFTEEN WINS
//IM LOST?? :(
import java.util.*;
import java.text.*;
import java.util.Random;
public class HorseRace{
public static void main(String[ ] arg){
final int ONE = 10;
final int TWO = 10;
int count = 0;
int i = 0; //position
String[ ] sWords = new String[ONE];
String sONEWord="";
Scanner sc = new Scanner(System.in);
System.out.println("Hit enter to begin race");
sc.nextLine();
Random ran = new Random( ); //generates random number
int[ ] arrRan1 = new int[ONE]; //populating the array one number at the time
for(int a = 0; a<arrRan1.length; a++){
arrRan1[a] = ran.nextInt(3) + 1; //number from 1 to 3, then add 1
}
boolean exit = false;
while(count < ONE){ for(int a = 0; a<arrRan1.length; a++){
System.out.println("Horse "+ (i+1) + ": " + (arrRan1[a]));
i = i +1;
}
System.out.print("Press Enter key to continue");
sc.nextLine();
Random ass = new Random( ); //generates random number
for(int b = 0; b<arrRan1.length; b++){
int a = 0;
arrRan1[b] = ran.nextInt(3) + 1; //number from 1 to 3, then add 1
}
boolean exit2 = false;
while(count < TWO){ for(int b = 0; b<arrRan1.length; b++){
System.out.println("Horse "+ (count+1) + ": " + (arrRan1[b]));
count = count +1;
}
}
}
}//main
} //class
Aucun commentaire:
Enregistrer un commentaire