所以我必须创建一个程序,我可以选择三张图片,每个图片都必须以不同的速度在屏幕上移动,然后程序输出第一名,第二名和第三名地点。 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