如何获取我的 2 个对象并调用一个函数来传递一个变量?

How do I get my 2 objects and call a function passing a variable through it?

在我的驱动程序 class CarRaceSim 的函数 race 中,我将我的两个对象都称为 car1 和 car2。然后我从我的 CarRace class 调用加速和制动函数,该函数应该从加速和制动中传递的速度变量中添加和减去一个随机数。 speed 变量是一个整数,用户可以为其赋值。但是,当我调用该函数并将其打印出来时,它只显示用户输入的内容。该程序通常应该模拟 5 场比赛,其中每一圈(循环 5 次)汽车加速和制动,这就是我称之为加速的原因并为两辆车刹车。我怎样才能使加速和刹车正常工作?

package carrace;
import java.util.Random;
/**
 *
 * @author Daniel
 */
public class CarRace {

    int year, speed;
    String model, make;


    // default constructor
    public CarRace(){
        year = 2000;
        model = "";
        make = "";
        speed = 0;
    }

    // non-default constuctor
    public CarRace(int aYear, String aModel, String aMake, int aSpeed){
        this.year = aYear;
        this.model = aModel;
        this.make = aMake;
        this.speed = aSpeed;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void accelerate(int speed){
        Random randomNum = new Random();
        int ranNum;

        ranNum = randomNum.nextInt(26) + 5;
        speed += ranNum; 
    }

    public void brake(int speed){
        Random randomNum = new Random();
        int ranNum;

        ranNum = randomNum.nextInt(26) + 5;
        speed -= ranNum; 
    }

    public String toString(){
        return "The year of the car is " + year + ", the model of the car is " + model + ", the make of the car is " + make + ", and the initial speed is " + speed + ".\n";
    }
}

package carrace;
import java.util.Scanner;
/**
 *
 * @author Daniel
 */
public class CarRaceSim {

    public static CarRace car1;
    public static CarRace car2;

    public static void main(String[] args) {

        System.out.println("Welcome to the car simulation program!");
        System.out.println("You will now begin to create your two cars to race, good luck!\n\n");

        createCar(1);
        createCar(2);

        race(car1, car2);
    }

    public static void createCar(int carCreation){
        Scanner keyboard = new Scanner(System.in);
        int year = 0;
        String model;
        String make;
        int speed = 0;

        do{
            if (carCreation == 1)
                System.out.println("Create your first car!");
            else
                System.out.println("Create your second car!");

            System.out.println("What year is your car?");
            year = keyboard.nextInt();

            System.out.println("What model is your car?");
            model = keyboard.next();

            System.out.println("What make is your car?");
            make = keyboard.next();

            System.out.println("What speed is your car initially starting at? (0-60)");
            speed= keyboard.nextInt();

            if(speed < 0){
                System.out.println("You can not begin at a negative speed, please choose between 0-60.");
            }
            else if(speed > 60){
                System.out.println("You can not start above 60, please choose between 0-60.");
            }          

        }while(speed <= 0 && speed >= 60);

        if(carCreation == 1){
            car1 = new CarRace(year, model, make, speed);
            System.out.println(car1);
        }
        else{
            car2 = new CarRace(year, model, make, speed);
            System.out.println(car2);
        }    

    }

    public static void race(CarRace carUno, CarRace carDue){

        for(int i = 1; i <= 5; i++){

            System.out.println("Lap " + i);

            System.out.println("Car 1's stats:");
            car1.accelerate(car1.getSpeed());
            System.out.println(car1.getSpeed());
            car1.brake(car1.getSpeed());
            System.out.println(car1.getSpeed());

            System.out.println("Car 2's stats:");
            car2.accelerate(car2.getSpeed());
            System.out.println(car2.getSpeed());
            car2.brake(car2.getSpeed());
            System.out.println(car2.getSpeed() + "\n");
        }
    }
}

在您的 accelerate() 函数中,您为局部变量 speed 赋值。您要分配给 class 变量 this.speed,就像您在其他方法中所做的一样(例如 setMake())。

也就是说变化

speed += ranNum;

this.speed += ranNum;

对你的刹车功能做同样的事情。

您在 accelerate()brake() 方法中更改的值在您传递的变量上更新,而不是在 class 的速度变量上更新。 要为 class 的速度变量分配新值,请在 accelerate()brake() 方法中添加以下行:

//for accelerate() method
//add below line after speed += ranNum;
this.speed = speed;

//for brake() method
//add below line after speed -= ranNum;
this.speed = speed;

另请参阅 Instance Variable Hiding 以进一步解释为什么需要这样做。

您应该如下更改加速方法:

public void accelerate(int speed){
        Random randomNum = new Random();
        int ranNum;

        ranNum = randomNum.nextInt(26) + 5;
        this.speed += ranNum; 
    }

同样你必须改变制动方式