英文:
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("Enter vehicle make");
String make = scanner.nextLine();
System.out.println("Enter vehicle model");
String model = scanner.nextLine();
for(Vehicle vehicle: vehicleList) {
if(make == vehicle.getMake() && model == vehicle.getModel()) {
System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
} else {
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();
for(Vehicle vehicle: vehicleList) {
if(minPrice <= vehicle.getPrice() && maxPrice >= vehicle.getPrice()) {
System.out.println(vehicle.getMake() + ";" + vehicle.getModel() + ";" + vehicle.getYear() + ";" + vehicle.getPrice());
break;
}
System.out.println("No vehicles in price range found");
}
}
}
答案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("Enter vehicle make");
String make = scanner.nextLine();
System.out.println("Enter vehicle model");
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("No such vehicle is found");
}
}
答案2
得分: 2
Java的==
用于引用比较,而.equals()
用于内容比较。在这种情况下,您正在比较内容,即make
和model
的值,应改用.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("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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论