增强型for循环,如果匹配则返回结果,如果不匹配则打印未找到。

huangapple go评论64阅读模式
英文:

Enhanced for loop, how to return result if matches and print out not found if not

问题

class CarDealer {
   private Vehicle[] vehicleList;

   private void searchMakeModel() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter vehicle make");
        String make = scanner.nextLine();
        System.out.println("Enter vehicle model");
        String model = scanner.nextLine();
        boolean vehicleFound = false;
        
        for(Vehicle vehicle: vehicleList) {
           if(make.equals(vehicle.getMake()) && model.equals(vehicle.getModel())) {
              System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
              vehicleFound = true;
              break; // Exit the loop after finding a match
           }
        }
        
        if (!vehicleFound) {
            System.out.println("No such vehicle is found");
        }
   }
   
   private void searchPriceRange() {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter min price: $");
      double minPrice = scanner.nextDouble();
      System.out.println("Enter max price: $");
      double maxPrice = scanner.nextDouble();
      boolean vehicleFoundInRange = false;
      
      for(Vehicle vehicle: vehicleList) {
         if(minPrice <= vehicle.getPrice() && maxPrice >= vehicle.getPrice()) {
            System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
            vehicleFoundInRange = true;
         }
      }
      
      if (!vehicleFoundInRange) {
         System.out.println("No vehicles in price range found");
      }
   }
}

注意:在Java中,字符串比较应该使用 .equals() 方法而不是 ==,因为 == 比较的是引用而不是内容。另外,为了避免多次输出 "No such vehicle is found" 和 "No vehicles in price range found",在循环内部找到匹配或在价格范围内的车辆后,我们可以设置一个标志来记录是否找到匹配的车辆,然后在循环外部根据这个标志来输出相应的信息。

英文:

I'm working on a java program for a car dealership. I'm using the enhanced for loop to loop through an array of vehicles and I want to return a match if I input a make and model that matches it. If it doesn't match, I want to return "no such vehicle is found" but only one time. Right now, it returns 9 "not founds" and 1 matching result. I only want to return matching results and if there are none, only 1 not found.

I have two functions searchMakeModel() and searchPriceRange(). My searchMakeModel() is supposed to be returning matching cars, but it just returns not found for some reason. My searchPriceRange() works, but I want it to only return cars. Right now, it returns both not found if the car doesn't match the price range and the car if the car matches the price range.

class CarDealer {
private Vehicle[] vehicleList;
private void searchMakeModel() {
Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Enter vehicle make&quot;);
String make = scanner.nextLine();
System.out.println(&quot;Enter vehicle model&quot;);
String model = scanner.nextLine();
for(Vehicle vehicle: vehicleList) {
if(make == vehicle.getMake() &amp;&amp; model == vehicle.getModel()) {
System.out.println(vehicle.getMake() + &quot;;&quot; + vehicle.getModel() + &quot;;&quot; + vehicle.getYear() + &quot;;&quot; + vehicle.getPrice());
} else {
System.out.println(&quot;No such vehicle is found&quot;);
}
}
}
private void searchPriceRange(){
Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Enter min price: $&quot;);
double minPrice = scanner.nextDouble();
System.out.println(&quot;Enter max price: $&quot;);
double maxPrice = scanner.nextDouble();
for(Vehicle vehicle: vehicleList) {
if(minPrice &lt;= vehicle.getPrice() &amp;&amp; maxPrice &gt;= vehicle.getPrice()) {
System.out.println(vehicle.getMake() + &quot;;&quot; + vehicle.getModel() + &quot;;&quot; + vehicle.getYear() + &quot;;&quot; + vehicle.getPrice());
break;
}
System.out.println(&quot;No vehicles in price range found&quot;);
}
}
}

答案1

得分: 2

private void searchMakeModel() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入车辆品牌");
    String make = scanner.nextLine();
    System.out.println("请输入车辆型号");
    String model = scanner.nextLine();
    boolean found = false;
    for(Vehicle vehicle: vehicleList) {
        if(make.equals(vehicle.getMake()) && model.equals(vehicle.getModel())) {
            System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
            found = true;
            break;
        }
    }
    if(!found){
        System.out.println("未找到该车辆");
    }
}
英文:
private void searchMakeModel() {
Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Enter vehicle make&quot;);
String make = scanner.nextLine();
System.out.println(&quot;Enter vehicle model&quot;);
String model = scanner.nextLine();
boolean found = false;
for(Vehicle vehicle: vehicleList) {
if(make.equals(vehicle.getMake()) &amp;&amp; model.equals(vehicle.getModel())) {
System.out.println(vehicle.getMake() + &quot;;&quot; + vehicle.getModel() + &quot;;&quot; + vehicle.getYear() + &quot;;&quot; + vehicle.getPrice());
found = true;
break;
}
}
if(!found){
System.out.println(&quot;No such vehicle is found&quot;);
}
}

答案2

得分: 2

Java的==用于引用比较,而.equals()用于内容比较。在这种情况下,您正在比较内容,即makemodel的值,应改用.equals()。另外,要确定是否找到了"the vehicle not",您应首先检查所有车辆。一个可能的解决方案如下:

private void searchMakeModel() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter vehicle make");
    String make = scanner.nextLine();
    System.out.println("Enter vehicle model");
    String model = scanner.nextLine();
    boolean isFound = false;
    for(Vehicle vehicle: vehicleList) {
        if(make.equals(vehicle.getMake()) && model.equals(vehicle.getModel())) {
            isFound = true;
            System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
        }
    }
    if(!isFound)
        System.out.println("No such vehicle is found");
}
英文:

Java == is used for reference comparison while .equals() is used for content comparison. As in this case you are comparing the content i.e. make and model value use .equals() instead. Also, to determine the vehicle not found you should check all the vehicles first. A possible solution looks like this:

private void searchMakeModel() {
Scanner scanner = new Scanner(System.in);
System.out.println(&quot;Enter vehicle make&quot;);
String make = scanner.nextLine();
System.out.println(&quot;Enter vehicle model&quot;);
String model = scanner.nextLine();
boolean isFound = false;
for(Vehicle vehicle: vehicleList) {
if(make.equals(vehicle.getMake()) &amp;&amp; model.equals(vehicle.getModel())) {
isFound = true;
System.out.println(vehicle.getMake() + &quot;;&quot; + vehicle.getModel() + &quot;;&quot; + vehicle.getYear() + &quot;;&quot; + vehicle.getPrice());
}
}
if(!isFound)
System.out.println(&quot;No such vehicle is found&quot;);
}

huangapple
  • 本文由 发表于 2020年10月4日 02:23:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定