我在java类的介绍中,我遇到了分配的汽车类的问题。以下是说明: 编写一个名为Car的类,它具有以下字段(属性):
yearModel
(一个持有汽车年份模型的int)
make
(一个保存汽车品牌的字符串)
speed
(保持汽车当前速度的int)
Car
类应具有以下构造函数和其他方法:
Constructor
- 接受汽车的year
model
和make
作为参数。
Accessors
,yearModel
和make
字段 speed
(getters)
方法:
accelerate
- 每次调用它时,都应该向speed
字段添加5
brake
- 每次调用时,都应将5减去speed
字段
编写Driver
类,DrivingSimulation
类,执行以下操作:
提示用户输入汽车的yearModel
,make
和speed
值。
创建一个Car
对象
拨打accelerate
方法5次 - 每次通话后,使用accessor
方法显示汽车的当前speed
拨打brake
方法5次 - 每次通话后,使用accessor
方法显示汽车的当前speed
我完成了大部分工作,但它没有收到我正在接收的速度输入,而是从默认值开始,0。这是我的车类
package drivingsimulation;
/**
*
* @author Carlos
*/
public class Car {
private int yearModel;
private String make;
private int speed;
public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
}
public int getYearModel() {
return yearModel;
}
public String getMake() {
return make;
}
public int getSpeed() {
System.out.println("You are going " + speed + " mph");
return speed;
}
public void accelerate() {
speed += 5;
}
public void brake () {
speed-=5;
}
}
这是我的司机班:
/*
package drivingsimulation;
import java.util.Scanner;
/**
*
* @author Carlos
*/
public class DrivingSimulation {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int yearModel=0;
int speed=0;
String make=null;
Scanner keyboard = new Scanner (System.in);
Car myCar = new Car(yearModel, make);
System.out.println("What is the year of your car? ");
yearModel = keyboard.nextInt();
System.out.println("What is the make of your car? ");
make = keyboard.next();
System.out.println("How fast is your car going? ");
speed = keyboard.nextInt();
for(int i=0; i<5; i++){
myCar.accelerate();
}
}
}
答案 0 :(得分:1)
当你从驱动程序类的用户输入中获取值时,你有速度类成员的访问者,但没有setter。
Car类中的方法如下:
public void setSpeed(int speed)
{
this.speed = speed;
}
是否给出了汽车实例的设定值是用户输入的。
你可以在用户输入这样的速度后调用它。
myCar.setSpeed(speed);
答案 1 :(得分:0)
你怎么知道它没有得到速度信息 - 你永远不会在上面打getSpeed()
。
建议:
getSpeed()
。public void setSpeed(int speed)
,然后调用方法。例如,getSpeed()
应该只是
public int getSpeed() {
return speed;
}
就是这样,仅此而已。但你会这样称呼:
System.out.println("Car's speed: " + myCar.getSpeed());
您也没有public void setSpeed(int speed)
方法,并且需要一个,并且应该在从用户那里获取信息后调用它。
答案 2 :(得分:0)
在你的Car类中添加一个setSpeed()
setter并在你的DrivingSimulation类中调用它,如下所示(也将print语句移动到DrivingSimulation,这样你的Car仍然模块化)。
public class Car {
private int yearModel;
private String make;
private int speed;
public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
}
public int getYearModel() {
return yearModel;
}
public String getMake() {
return make;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void accelerate() {
speed += 5;
}
public void brake () {
speed -= 5;
}
}
和...
import java.util.Scanner;
public class DrivingSimulation {
public static void main(String[] args) {
int yearModel = 0;
String make = null;
int speed;
Scanner keyboard = new Scanner(System.in);
Car car = new Car(yearModel, make);
System.out.println("What is the year of the car?");
yearModel = keyboard.nextInt();
System.out.println("What is the make of the car?");
keyboard.nextLine(); // fixes bug - receives the Enter key without skipping make = keyboard.nextLine()
make = keyboard.nextLine();
System.out.println("What speed is the initial speed of the car?");
speed = keyboard.nextInt();
car.setSpeed(speed);
for (int i = 0; i < 5; i ++) {
car.accelerate();
System.out.println("Your " + yearModel + " " + make +
" is traveling at " + car.getSpeed() + " MPH.");
}
}
}