有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java汽车类程序

下面是给我的驱动程序类,我不允许编辑/更改此类

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());
   }
}

这就是那辆车。我创建的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和最后一辆车4未显示,因为驾驶员类别为空,我无法更改检测仪类别,只能更改我的汽车类别

输出应该是什么样子的

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

编辑 我做了推荐给我的更改,但由于某种原因,我收到了一条关于字符串到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);
                     ^

共 (2) 个答案

  1. # 1 楼答案

    问题在于你的default constructor

    public Car()
    {
       year = 0;
       brand = null;
       price = 0;
       number = 0;
    }
    

    所有内容都设置为0null,这意味着您的实际输出(尽管在撰写本文时您尚未发布)可能类似于:

    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
    

    或者类似的东西。这是因为您已将所有成员变量设置为0null

    尝试将默认构造函数更改为以下内容:

    public Car()
    {
       // This car is Chevy, year 2005, price 3000
       year = 2005;
       brand = "Chevy";
       price = 3000;
       number = 4;
    }
    

    这样,当您实例化(即创建)一个没有参数的对象时(如car 1和car 4),它会自动为您填充这些值

    EDIT:正如Mohd Akbar指出的,您的代码还有另一个问题,那就是numberinstance 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)
    

    这将解决你面临的另一个问题

  2. # 2 楼答案

    您需要做的第一件事是将变量“number”设置为静态变量,以便该类的所有对象共享它,而不是为每个对象创建副本。 您需要在构造函数中增加'number',而不仅仅是参数化构造函数

    private static int number;
    

    在构造函数和

    number++;
    

    您创建的参数化构造函数是正确的,“Chipster”的默认构造函数是正确的

    希望,我已经说清楚了