2015-12-20 72 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