public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度為"+red);
System.out.println("green的速度為"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵達終點線");
}
if(green >= 500){
System.out.println("green先抵達終點線");
}
if(green ==500 && red ==500 ){
System.out.println("兩個同時到達");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/
}
}//給妳個思路自己用swing畫出來,這個小程序是把速度設為2紅綠兩只馬先超過500這壹線的獲勝.如果要多人比賽開啟線程就好