I am making a horse race program and I have to display something like
Horse 1: 0
Horse 2: 0
Horse 3: 0
The horse "positions" are in an array and I will be using a ran num generator to generate a number from 1-3 and then that will be added to the previous positions to simulate the horses moving. Is there a way to make it so that the display updates the new positions rather than printing all of the positions again? This is an example of the code that I simplified to show how it prints twice. Also, is there a way to loop the adding until one of the horses wins at 15? Would I use a while
statement? Thank you in advance.
import java.util.*;
public class ArrayRandom{
public static void main(String[ ] args){
final int SIZE = 3;
Random ran = new Random( );
int[ ] arrRan = new int[SIZE];
//populating the array one number at the time
for(int i = 0; i<arrRan.length; i++){
arrRan[i] = ran.nextInt(3) + 1;
}
System.out.println("The horse positions are:");
System.out.println("Horse 1: " + arrRan[0]);
System.out.println("Horse 2: " + arrRan[1]);
System.out.println("Horse 3: " + arrRan[2]);
for(int i = 0; i<arrRan.length; i++){
arrRan[i] = arrRan[i] + (ran.nextInt(3) + 1);
}
System.out.println("The horse positions are:");
System.out.println("Horse 1: " + arrRan[0]);
System.out.println("Horse 2: " + arrRan[1]);
System.out.println("Horse 3: " + arrRan[2]);
for(int i = 0; i<arrRan.length; i++){
arrRan[i] = arrRan[i] + (ran.nextInt(3) + 1);
}
System.out.println("The horse positions are:");
System.out.println("Horse 1: " + arrRan[0]);
System.out.println("Horse 2: " + arrRan[1]);
System.out.println("Horse 3: " + arrRan[2]);
}
}
Aucun commentaire:
Enregistrer un commentaire