2015-12-20 43 views
0

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

}

   } 




      } 

}

+0

更改您的标题以切合您的问题。 – csmckelvey

回答

0

其实我刚拍完神似你所描述的,但没有复杂的

我把while循环中的这些方法的东西

race.moveRacer1(xPos1,100); 
race.moveRacer2(xPos2,300); 
race.moveRacer3(xPos3,500); 

race.repaint(); 

然后我也使用math.random类来增加他们沿屏幕行进的距离,这将使它随机比赛,每次我可以发布代码,如果你喜欢。

编辑:代码从以前的游戏循环

这只是游戏循环IM不会发布其他类,因为它会变成一个新的,但是这应该让你离开的麻烦。

public void race() throws InterruptedException{ 

    while (counter < 21){ 

    random = Math.random() * 5; 
    horse1 = (int) (horse1 + random); 

    random = Math.random() * 5; 
    horse2 = (int) (horse2 + random); 

     if(horse1 < horse2 & counter == 2){ 
      System.out.println("There off Maldiean takes the lead"); 
     }else if(horse1 > horse2 & counter == 2){ 
      System.out.println("There off Spark Of Life takes the lead"); 
     }else if(horse1 > horse2 & counter == 10){ 
      System.out.println("As they come around the bend Spark of life in the lead"); 
     }else if(horse1 < horse2 & counter == 10){ 
      System.out.println("As they come around the bend Maldiean's in the lead"); 
     }else if(horse1 > horse2 & counter == 15){ 
      System.out.println("On the home strech Spark of life in the lead"); 
     }else if(horse1 < horse2 & counter == 15){ 
      System.out.println("On the home strech Maldiean in the lead"); 
     }else if(horse1 > horse2 & counter >= 20){ 
      System.out.println("Spark of life Wins"); 
     }else if(horse1 < horse2 & counter >= 20){ 
      System.out.println("Maldiean Wins"); 
     } 
     move(); 
     repaint(); 
     Thread.sleep(500); 
    counter ++; 
    } 
} 
+0

如果我能得到代码,这将是非常好的......只是我不明白如何将math.random应用到这个循环中。 – Aaron