【问题标题】:How to search through an array and check for a condition in Java如何在 Java 中搜索数组并检查条件
【发布时间】:2021-05-11 16:21:36
【问题描述】:

我有一系列新车和二手车。我想在数组中搜索颜色为绿色的汽车。方法printAllCarsOfColor(String color) 将返回一个字符串,该字符串显示作为参数给出的颜色的所有汽车的信息。我试过了,但没有运气。

我有 Car 类,它是超类,NewCarUsedCar 是子类。只有NewCar 类有Color 方法。我不知道如何打印出只有绿色的汽车。我知道 Color 方法不是为 Car 类定义的。

寻求帮助。谢谢!

我的代码:

public class CarDearlerShip
{
   private Car[] aCar;
   private int count;
   private int car;
  
   public CarDearlerShip()
   {
     aCar = new Car[80];
      count = 0;
      car = 0;  
   }
  
   
   public void addNewCar(String model, int year, int price, String color)
   {
      aCar[count] = new NewCar(model, year, price, color);
      count++;
   }   
   
   public void addUsedCar(String model, int year, int price, boolean rusty)
   {
      aCar[count] = new UsedCar(model, year, price, rusty);
      count++;
   } 
   
   public String printReport()
   {
      String report = "---------------------------------------------\n";
      report += "The list of availabel cars:\n\n";
      
     for (int car = 0; car < count; car++)
      report += aCar[car].toString() + "\n";
      //System.out.println(report);
    return report;
      
   }
   
  public String printAllCarsWithSellingPriceBelow(int price) 
   {
      String printCars = "---------------------------------------------\n";
      printCars += "The price for these cars are: \n\n";
      
      for (int car = 0; car < count; car++)
      {
          if (aCar[car].aPrice < 1000); // The argument for the selling price
          printCars +=aCar[car].getPrice();
      }
         return printCars;       
   
   }
   
   public String printAllCarsOfColor(String color)
   {
      String printColor = "---------------------------------------------\n";
      printColor += "The color : \n\n";
      
      
      for (int car = 0; car < count; car++)
      {
         if (aCar[car].Color()==color);
         printColor +=aCar[car];
         
      }
      return printColor;      
   
   }
}

The Test Class
    
public class TestDealer 
{
    public static void main(String[] args)
    {
        CarDearlerShip dealer= new CarDearlerShip();
        dealer.addNewCar("GM Buick Century", 2004, 200000, "Silver");
        dealer.addUsedCar("Toyota Corolla", 1999, 9000, true);
        dealer.addNewCar("Honda Civic", 2004, 500, "Green");
        dealer.addNewCar("BMW 320i", 2004, 35000, "Black");
        dealer.addUsedCar("Toyota Sienna", 2000, 11000, false);
        
            System.out.println(dealer.printReport());
            System.out.println("**********************************");
        
        //System.out.println(dealer.printAllCarsWithSellingPriceBelow(1000));
            
            System.out.println("****************************************");

        System.out.println(dealer.printAllCarsOfColor("Green"));
        
        
    }

}

【问题讨论】:

  • 分享您的CarUsedCarNewCar 来源?
  • 看起来像是一个错字:if (aCar[car].Color()==color); 后面不应该有分号 (;)。你应该使用.equals 作为字符串。
  • 我认为颜色应该是 Car 而不是 NewCar 的属性,这会让事情变得更容易。

标签: java arrays sorting arraylist methods


【解决方案1】:

当您比较汽车的 Color 方法的输出时,您需要使用内置的 java.lang.String 方法 .equals()。

在您的代码中,您有:if(aCar[car].Color()==color){

但必须是if(aCar[car].Color().equals(color)){

基本上,编译器永远不会理解您在说什么,因为字符串变量“Color”只是指向计算机内存中存储值“green”的空间的指针。因此,当您将“绿色”与该指针进行比较时,它们将永远不会相同。

【讨论】:

  • 感谢您的回复尼克。我现在明白我的错误了。但是,当我尝试运行该程序时,出现以下错误: The method Color() is undefined for the type Car
  • 是的。在您的 NewCar/UsedCar 类中,您需要创建一个 getColor 方法。或者您可以像拥有它一样将其称为颜色。该方法应采用您在创建新车时存储的实例变量颜色并将其返回。那么你的程序应该可以正常工作了
  • 在您的 NewCar/UsedCar 类中应该看起来像这样。 public String getColor(){ return this.color; }
  • 我完全同意@NomadMaker。我已经测试了你的建议并且它有效。但是,我正在做一个类项目,它在细节中提到如下:类 Car 应该有一个构造函数 Car(String model, int year, int price),类 NewCar 有构造函数 NewCar(String model, int year, int price, String color), class UsedCar 有构造函数 UsedCar(String model, int year, int price, boolean rusty) 我想知道是否有另一种方法来满足描述中提到的要求。
  • 看看这个gist。这很糟糕,但这是可能的。我认为你的任务不应该通过继承来解决。任何汽车都有颜色,任何汽车都可能生锈,所以这些字段必须在汽车类中。那么扩展 Car 类就没有意义了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2021-02-17
  • 2016-08-02
  • 1970-01-01
  • 2017-04-16
相关资源
最近更新 更多