简体   繁体   English

如何搜索数组并检查 Java 中的条件

[英]How to search through an array and check for a condition in Java

I have an array of both new cars and used cars.我有很多新车和二手车。 I want to search through the array for the cars who have the color green.我想在数组中搜索颜色为绿色的汽车。 The method printAllCarsOfColor(String color) will return a string which displays the information of all cars of a color given as argument.方法printAllCarsOfColor(String color)将返回一个字符串,该字符串显示作为参数给出的颜色的所有汽车的信息。 I have tried but no luck.我试过但没有运气。

I have Car class which is the superclass and NewCar and UsedCar that are subclasses.我有 Car class 是超类, NewCarUsedCar是子类。 Only NewCar class has the Color method.只有NewCar class 有Color方法。 I can't figure out how to print out the cars that are only green.I understand that the method Color is not defined for Car class.我不知道如何打印出只有绿色的汽车。我知道方法Color没有为Car class 定义。

Looking for some help.寻求帮助。 Thanks!谢谢!

My code:我的代码:

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

}

When you are comparing the output of the Color method that your car has you need to be using the built in java.lang.String method.equals().当您比较您的汽车的颜色方法的 output 时,您需要使用内置的 java.lang.String method.equals()。

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

But it needs to be if(aCar[car].Color().equals(color)){但它必须是if(aCar[car].Color().equals(color)){

Basically the compiler is never going to understand what you are saying because the String variable "Color" is just a pointer to a space in your computer's memory where it is storing the value "green."基本上,编译器永远不会理解您在说什么,因为字符串变量“Color”只是指向计算机 memory 中存储值“green”的空间的指针。 So when you compare "green" to that pointer they will never be the same.因此,当您将“绿色”与该指针进行比较时,它们将永远不会相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM