下面是给我的驱动程序类,不允许我编辑/更改该类
public class HW2tester
{
public static void main(String[] args)
{
Car car1 = new Car();
Car car2 = new Car("Ford", 2013, 20000);
Car car3 = new Car("Audi", 2012, 25000);
Car car4 = new Car();
car2.setPrice(22000);
car2.setYear(2011);
car4.setBrand("Cadillac");
System.out.println("This car is " + car1.getBrand() + ", year " + car1.getYear() + ", price " + car1.getPrice());
System.out.println("This car is " + car2.getBrand() + ", year " + car2.getYear() + ", price " + car2.getPrice());
System.out.println("This car is " + car3.getBrand() + ", year " + car3.getYear() + ", price " + car3.getPrice());
System.out.println("This car is " + car4.getBrand() + ", year " + car4.getYear() + ", price " + car4.getPrice());
System.out.println("The total car number is: " + car1.getNumber());
System.out.println("The total car number is: " + car2.getNumber());
System.out.println("The total car number is: " + car3.getNumber());
System.out.println("The total car number is: " + car4.getNumber());
}
}
这是我创建的Car.java类文件
public class Car
{
private int year;
private String brand;
private int price;
private int number;
public Car()
{
year = 0;
brand = null;
price = 0;
number = 0;
}
public Car( int y, String b, int p)
{
number++;
year = y;
brand = b;
price = p;
}
public void setYear( int y)
{
year = y;
}
public void setBrand( String b)
{
brand = b;
}
public void setPrice( int p)
{
price = p;
}
public int getYear()
{
return year;
}
public String getBrand()
{
return brand;
}
public int getPrice()
{
return price;
}
public int getNumber()
{
return number;
}
}
我遇到的问题: 试图合并计数以显示汽车总数(4) 由于驾驶员级别为空,因此第一个汽车对象car1和最后一个car4没有显示,我无法更改测试人员级别,只能更改我的汽车级别。
应该是什么样的输出
This car is Chevy, year 2005, price 3000
This car is Ford, year 2011,price 22000
This car is Audi, year 2012, price 25000
This car is Cadillac, year 2005, price 3000
The total car number is: 4
The total car number is: 4
The total car number is: 4
The total car number is: 4
EDIT 我已经向我推荐了所做的更改,但是由于某种原因,我收到有关将字符串转换为Int的错误消息。这是我所做的更改以及我收到的错误消息。
public class Car
{
int year;
String brand;
int price;
static int number;
public Car()
{
year = 2005;
brand = "Chevy";
price = 3000;
number++;
}
HW2tester.java:6: error: incompatible types: String cannot be converted to int
Car car2 = new Car("Ford", 2013, 20000);
^
HW2tester.java:7: error: incompatible types: String cannot be converted to int
Car car3 = new Car("Audi", 2012, 25000);
^
答案 0 :(得分:1)
您需要做的第一件事是使变量“ number”成为静态变量,以便该类的所有对象共享该变量,而不是为每个对象创建一个副本。 而且您不仅需要在参数化的构造函数中,而且在两个构造函数中都需要增加“ number”。
private static int number;
在这两个构造函数中,您都需要做
number++;
您创建的参数化构造函数正确,而'Chipster'的默认构造函数正确。
希望,我已经说清楚了。
答案 1 :(得分:0)
问题出在您的default constructor:
public Car()
{
year = 0;
brand = null;
price = 0;
number = 0;
}
所有内容均设置为0
或null
,这意味着您的实际输出(尽管在撰写本文时您尚未发布)可能类似于:
This car is , year 0, price 0
This car is Ford, year 2011,price 22000
This car is Audi, year 2012, price 25000
This car is Cadillac, year 0, price 0
The total car number is: 0
The total car number is: 4
The total car number is: 4
The total car number is: 0
或者类似的东西。这是因为您已将所有成员变量都设置为0
或null
。
尝试将默认构造函数更改为以下形式:
public Car()
{
// This car is Chevy, year 2005, price 3000
year = 2005;
brand = "Chevy";
price = 3000;
number = 4;
}
这样,当您实例化(即创建)不带参数的对象时(如汽车1和汽车4一样),它将自动为您填充这些值。
编辑:正如Mohd Akbar指出的那样,您的代码还存在另一个问题,即number
是instance variable,而不是static one
您可能希望将数字更改为更像this example,如下所示:
static int number = 0;
并更改您的构造函数以使其匹配:
public Car()
{
// This car is Chevy, year 2005, price 3000
year = 2005;
brand = "Chevy";
price = 3000;
number++;
}
代替我最初的建议number = 4;
编辑2:您可能遇到的另一个问题是您的参数在其他构造函数上的排列顺序不正确:
public Car( int y, String b, int p)
您的另一堂课(给您的课)期望这样的参数:
public Car(String b, int y, int p)
那应该可以解决您遇到的另一个问题。