2015-10-30 21 views
2
public class Race { 

public static void main(String[] args) { 
int maxSpeed = Car.getMaxSpeedForAll(); 
    int raceLength = 1000; 

    Car mario = new Car(30, "Mario"); 
    Car luigi = new Car(30, "Luigi"); 

    while(mario.getLocation() < raceLength || luigi.getLocation() < raceLength){ 
      mario.randomSpeedChange(); 
      luigi.randomSpeedChange(); 
      if (mario.getLocation() > luigi.getLocation()){ 
       System.out.println(mario + " is in first place at " + mario.getLocation() + "!"); 
      } 
      if (luigi.getLocation() > mario.getLocation()){ 
       System.out.println(luigi + " is in first place at " + mario.getLocation() + "!"); 
      } 
      if (mario.getLocation() == luigi.getLocation()){ 
       System.out.println(mario + luigi + "are neck and neck! Who will pull ahead and take the lead?!"); 
      } 
     } 
    } 
} 


import java.util.Random; 

public class Car { 
    private int speed; 
    private String carName; 
    private int location = 0; 
    Random r = new Random(); 
    private int rand; 

    private static int maxSpeedForAll = 120; 
    private static int minSpeedForAll = 0; 

    public Car(int speed, String name) { 
     this.speed = speed; 
     this.carName = name; 
    } 

    public int getLocation(){ 
     return location; 
    } 

    public void setLocation(int location, int speed){ 
     this.location += speed; 
    } 

    public static int getMaxSpeedForAll(){ 
     return maxSpeedForAll; 
    } 

    public static void setMaxSpeedForAll(int maxSpeedForAll){ 
     Car.maxSpeedForAll = maxSpeedForAll; 
    } 

    public static int getMinSpeedForAll(){ 
     return minSpeedForAll; 
    } 

    public static void setMinSpeedForAll(int minSpeedForAll){ 
     Car.minSpeedForAll = minSpeedForAll; 
    } 

    public int getSpeed(){ 
     return speed; 
    } 

    public void setSpeed(int speed){ 
     if(speed <= maxSpeedForAll){ 
      this.speed = speed; 
     } else { 
      this.speed = maxSpeedForAll; 
     } 
     if(speed < minSpeedForAll){ 
      this.speed = minSpeedForAll; 
     } 
    } 

    public String getName(){ 
     return carName; 
    } 

    public void setName(String name){ 
     this.carName = name; 
    } 

    public String toString(){ 
     String result; 
     result = carName; 
     return result; 
    } 

    public void accelerate(int rand){ 
     if (rand > 0){ 
      speed += 10; 
     } 
    } 

    public void decelerate(int rand){ 
     if (rand < 0){ 
      speed += -10; 
     } 
    } 

    public void randomSpeedChange(){ 
     setRand(r.nextInt(10 - -10) + -10); 
    } 

    public int getRand() { 
     return rand; 
    } 

    public void setRand(int rand) { 
     this.rand = rand; 
    } 
} 

所以我現在正在做一個任務,我需要在設定的比賽長度上比賽兩輛賽車。爲了確定賽車的當前位置,我必須將賽車的位置值添加到賽車的速度值中,並且該值必須顯示在主賽道上。我認爲很簡單,但是賽車的速度每次在-10到10的時間間隔內都會隨機變化。我一直在爲這幾天做出不同的解決方案,而且我沒有更接近實際做出這一點代碼的工作原理是如何工作的,如果有人能給我一些關於我做錯了什麼的指示,我將不勝感激。在方法中添加一個int,並在主方法中顯示它

總之,我試圖: 1)將整數(位置)添加到整數(速度)並將其返回給主。 2)在將其添加到其他整數(位置)之前,在每個循環中,通過在-10和10之間隨機添加/減去來更改一個整數(速度)。

我提前道歉,如果這是回答已經某處 - 我已經搜索這個網站的高低與解決類似的問題,並不能真正找到任何這是同樣的情況我的問題。

回答

2

一個可能的結果有幾件事情:

  1. rand值被確定後,應該進行檢查,看是否要打電話acceleratedecelerate。這些方法應該修改speed值。
  2. acceleratedecelerate應使用修改後的值speed作爲參數調用setSpeed
  3. 速度設定後,setLocation應該叫。如果setLocation方法location參數將不被使用,它不需要被列爲正式的參數。
0

此行不會編譯:

System.out.println(mario + luigi + "are neck and neck! Who will pull ahead and take the lead?!"); 

您需要使用您的toString()方法,以使這項工作。我也建議添加「和」字,以及空格,因爲它目前輸出

你的代碼是目前編寫方式「MarioLuigiare並駕齊驅(等)!」; mario.speedluigi.speed從不修改,保持整個比賽中速度不變。您需要撥打accelerate()decelerate()方法或在randomSpeedChange()中包含代碼以修改其速度

您也永遠不會改變它們的位置,使它們始終保持在起跑線上。你應該改變它們的速度來調整setLocation()方法來解決這個問題。

我也建議修改decelerate()方法,以確保speed永遠不會低於零,除非您打算讓他們能夠倒退(第一次在解決上述問題後運行它,他們結束了巨大的負數爲location)。

這裏是我的代碼所做的修改(除了增加toString()在我的答案上提到的線):

randomSpeedChange()方法:

public void randomSpeedChange(){ 
    setRand(r.nextInt(10 - -10) + -10); 
    accelerate(rand); 
    decelerate(rand); 
} 

while環路主方法:

while(mario.getLocation() < raceLength || luigi.getLocation() < raceLength){ 
     mario.randomSpeedChange(); 
     mario.setLocation(); 
     luigi.randomSpeedChange(); 
     luigi.setLocation(); 
     //all the if statements here 
} 

setLocation()方法:

public void setLocation(){ 
    location += speed; 
} 

(這種方法工作得很好,但參數是不必要的。)

decelerate()方法:

public void decelerate(int rand){ 
    if ((rand < 0) && (speed >= 10)){ 
     speed += -10; 
    } 
} 

這確保了speed至少10減少之前,要確保它永遠不會低於0.

0

看來你不會改變循環塊中的速度和位置。我想這也許你需要什麼:

主要類:

public class Race { 

    public static void main(String[] args) { 
     int maxSpeed = Car.getMaxSpeedForAll(); 
     int raceLength = 1000; 

     Car mario = new Car(30, "Mario"); 
     Car luigi = new Car(30, "Luigi"); 

     while(mario.getLocation() < raceLength || luigi.getLocation() < raceLength){ 
      if (mario.getLocation() > luigi.getLocation()){ 
       System.out.println(mario + " is in first place at " + mario.getLocation() + "!"); 
      } 
      if (luigi.getLocation() > mario.getLocation()){ 
       System.out.println(luigi + " is in first place at " + mario.getLocation() + "!"); 
      } 
      if (mario.getLocation() == luigi.getLocation()){ 
       System.out.println(mario.getName() + luigi.getName() + "are neck and neck! Who will pull ahead and take the lead?!"); 
      } 

      //change the speed randomly 
      mario.randomSpeedChange(); 
      luigi.randomSpeedChange(); 
      //change the location according to speed 
      mario.setLocation(mario.getLocation(), mario.getSpeed()); 
      luigi.setLocation(luigi.getLocation(), luigi.getSpeed()); 
     } 
    } 
} 

車類:

import java.util.Random; 

public class Car { 
    private int speed; 
    private String carName; 
    private int location = 0; 
    Random r = new Random(); 
    private int rand; 

    private static int maxSpeedForAll = 120; 
    private static int minSpeedForAll = 0; 

    public Car(int speed, String name) { 
     this.speed = speed; 
     this.carName = name; 
    } 

    public int getLocation(){ 
     return location; 
    } 

    public void setLocation(int location, int speed){ 
     this.location += speed; 
    } 

    public static int getMaxSpeedForAll(){ 
     return maxSpeedForAll; 
    } 

    public static void setMaxSpeedForAll(int maxSpeedForAll){ 
     Car.maxSpeedForAll = maxSpeedForAll; 
    } 

    public static int getMinSpeedForAll(){ 
     return minSpeedForAll; 
    } 

    public static void setMinSpeedForAll(int minSpeedForAll){ 
     Car.minSpeedForAll = minSpeedForAll; 
    } 

    public int getSpeed(){ 
     return speed; 
    } 

    public void setSpeed(int speed){ 
     if(speed <= maxSpeedForAll){ 
      this.speed = speed; 
     } else { 
      this.speed = maxSpeedForAll; 
     } 
     if(speed < minSpeedForAll){ 
      this.speed = minSpeedForAll; 
     } 
    } 

    public String getName(){ 
     return carName; 
    } 

    public void setName(String name){ 
     this.carName = name; 
    } 

    public String toString(){ 
     String result; 
     result = carName; 
     return result; 
    } 

    public void accelerate(int rand){ 
     if (rand > 0){ 
      speed += 10; 
     } 
    } 

    public void decelerate(int rand){ 
     if (rand < 0){ 
      speed += -10; 
     } 
    } 

    public void changeSpeed(int rand) { 
     if (rand > 0){ 
      speed += 10; 
     } 
     if (rand < 0){ 
      speed += -10; 
     } 
    } 

    public void randomSpeedChange(){ 
     setRand(r.nextInt(10 - -10) + -10); 
     changeSpeed(rand); 
    } 

    public int getRand() { 
     return rand; 
    } 

    public void setRand(int rand) { 
     this.rand = rand; 
    } 
} 
相關問題