gpt4 book ai didi

java - 如何获取我的 2 个对象并调用通过它传递变量的函数?

转载 作者:行者123 更新时间:2023-12-01 16:52:49 25 4
gpt4 key购买 nike

在函数 race 的驱动程序类 CarRaceSim 中,我将两个对象称为 car1 和 car2。然后,我从 CarRace 类中调用加速和制动函数,该函数应该从加速和制动中传递的速度变量中添加和减去一个随机数。 speed 变量是一个由用户赋值的 int 变量。但是,当我调用该函数并将其打印出来时,它仅显示用户输入的内容。该程序通常应该模拟 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 赋值。您想要分配给类变量 this.speed,就像您在其他方法中所做的那样(例如 setMake())。

换句话说,改变

speed += ranNum;

this.speed += ranNum;

对制动功能执行相同的操作。

关于java - 如何获取我的 2 个对象并调用通过它传递变量的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com