所以我必須創建一個程序,我可以選擇三張圖片,每個圖片都必須以不同的速度在屏幕上移動,然後程序輸出第一名,第二名和第三名地點。 while循環也必須運行3次(3場比賽)。到目前爲止,我已經完成了比賽,所以比賽只完成一次,但是我每次都會隨機制作每場比賽的麻煩。每次我要現在參加比賽時,結果都不會改變。你如何做到這一點,以便比賽的結果實際上是隨機的?另外,你如何讓循環運行3次?整個屏幕上的圖片賽車
public class HeadCoachRacing2{
public static void main(String[] args){
RacerCanvas race = new RacerCanvas (800,800);
int xPos1 = 1;
int xPos2 = 1;
int xPos3 = 1;
int x = 1;
String first="";
String second="";
String third="";
race.setFiles("im1.jpg", "im2.png", "im3.png");
race.moveRacer1(xPos1,100);
race.moveRacer2(xPos2,300);
race.moveRacer3(xPos3,500);
race.repaint();
race.showText(false);
while ((xPos1<=650) && (xPos2<=650) && (xPos3<=650))
{
xPos1 +=(int)(Math.random()*3)+1;
if (xPos1>=650){
xPos1= 650;}
race.moveRacer1(xPos1,100);
xPos2 +=(int)(Math.random()*1)+1;
if (xPos2>=650){
xPos2=650;}
race.moveRacer2(xPos2,300);
xPos3 +=(int)(Math.random()*2)+1;
if (xPos3>=650){
xPos3=650;}
race.moveRacer3(xPos3,500);
race.delay(5);
race.repaint();
if ((xPos1==650) &&(xPos1>xPos2)&&(xPos2>xPos3)){
first="Bill Belichick";
second="Gary Kubiak/Peyton Manning";
third="Rob Ryan";
}
else if ((xPos2==650)&&(xPos1<xPos2)&&(xPos3<xPos1)){
first="Gary Kubiak/Peyton Manning";
second="Bill Belichick";
third="Rob Ryan";
}
else if ((xPos3==650)&&(xPos1<xPos3)&&(xPos2<xPos1)){
first="Rob Ryan";
second="Bill Belichick";
third="Gary Kubiak/Peyton Manning";
}
else if ((xPos1==650)&&(xPos3<xPos1)&&(xPos2<xPos3)){
first="Bill Belichick";
second="Rob Ryan";
third="Gary Kubiak/Peyton Manning";
}
else if ((xPos2==650)&&(xPos3<xPos2)&&(xPos1<xPos3)){
first="Gary Kubiak/Peyton Manning";
second="Rob Ryan";
third="Bill Belichick";
}
else if ((xPos3==650)&&(xPos2<xPos3)&&(xPos1<xPos2)){
first="Rob Ryan";
second="Gary Kubiak/Peyton Manning";
third="Bill Belichick";
}
if ((xPos1==650) && (xPos2==650) && (xPos3==650)){
race.showText(true);
race.setPlaces(first+ "is in First Place!" +third " is in Second Place! "+ second + " is in Third Place!", 100,100, 30);
race.delay(1000);
race.repaint();
}
}
}
}
更改您的標題以切合您的問題。 – csmckelvey