如何将随机数添加到ArrayList对象中并确定谁越过该行

时间:2017-04-11 07:23:14

标签: java arraylist

我想在每个ArrayList对象中添加0到50的随机数 对象将尝试到达最后一行10000米。

我该怎么做。
这是我的代码:

private static final int maxDistance = 10000; 
private ArrayList<CarModel> theCar;

public CarController() {

    this.theCar = new ArrayList<CarModel>();     

}
//adding object into the ArrayList

public void addCar(String driverName)
{

    CarModel c = new CarModel(driverName);

     theCar.add(c);

}

// Race Start method

public void raceStart()
{

     while( theCar.size() != maxDistance )
     {
         for(int i=0;i<theCar.size();i++)
         {

            theCar.add(randomWithRange(0, 50));
         }
     }

//random number generated method 

public static int randomWithRange(int min, int max)  
{
    int range = (max - min) + 1;
    return (int) (Math.random() * range) + min;
}

此部分是编辑的代码。在这里,我希望得到其他车手比赛细节,他们花了多长时间与winer一起完成比赛。

public void raceStart() {

    while (true) {
        for (CarModel car : theCar) {
            int carDistance = car.getCarDistance();  
            carDistance += randomWithRange(0, 50);
            car.setCarDistance(carDistance);  

            for (int time = 0; time <= maxDistance; time++)  
            {

                if (carDistance >= maxDistance) {
                    System.out.println("First winner is :" + car.getDriverNames() + "\nTOTAL DISTANCE TRAVELED : "
                            + carDistance + " METERS " + "\nTOTAL TIME TO FINISH THE RACE : " + time);


                    System.out.println("\n\nALL OTHERS COMPETITORS RACE DETAILS");

                    for (int t = 0; t < carDistance; t++) // will find out
                                                            // all other
                                                            // drivers time
                                                            // and distance
                    {
                        System.out.println(
                                "DRIVER NAME :" + car.getDriverNames() + "\nCAR DISTANCE : " + carDistance + "\n");
                        System.out.println();
                    }
                    return; // Race has finished

                }

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你,你应该将字段“距离”添加到你的CarModel类并在每次迭代中递增它:

public void raceStart() {
    while (true) {
        for (CarModel car: theCar) {
            int carDistance = car.getDistance();   // Get car's current distance
            carDistance += randomWithRange(0, 50);
            car.setDistance(carDistance);          // And set in back
            if (carDistance >= maxDistance) {
                System.out.println(car.getDriverName());
                return;                          // Race has finished
            }
        }
    }
}

<强> UPD 上面的代码有一个逻辑错误,因为我们应该完成当前的迭代以找出谁是胜利者(否则参与者有不同的距离增量数)。这是更新的代码:

public void raceStart() {
    long startTime = System.currentTimeMillis();
    long finishTime;
    CarModel winner = null;
    while (true) {
        if (winner != null) {   // Race has finished
            processResults(winner, finishTime - startTime);
            return;
        }
        for (CarModel car : theCar) {
            int carDistance = car.getCarDistance();
            carDistance += randomWithRange(0, 50);
            car.setCarDistance(carDistance);
            if (carDistance >= maxDistance) {                 
                if (winner == null || carDistance > winner.getCarDistance()) {
                    winner = car;   // We have new winner
                    finishTime = System.currentTimeMillis();
                }
            }
        }
    }
}

private void processResults(CarModel winner, long winnerTime) {
    System.out.println("First winner is :" + winner.getDriverNames() + "\nTOTAL DISTANCE TRAVELED : "
            + winner.getCarDistance() + " METERS " + "\nTOTAL TIME TO FINISH THE RACE : " + winnerTime);

    System.out.println("\n\nALL OTHERS COMPETITORS RACE DETAILS");
    for (CarModel car : theCar) {
        if (!car.equals(winner)) {// need to skip winner
            System.out.println(
                    "DRIVER NAME :" + car.getDriverNames() + "\nCAR DISTANCE : " + car.getDistance() + "\n");
            System.out.println();
        }
    }
}

当至少一辆车达到终点时,获胜者参考不会为空。我们需要在下一次汽车迭代之前打破While循环,因为竞赛已经完成。希望你理解我的想法