gpt4 book ai didi

java - 无法让我的代码从 ArrayList 返回对象

转载 作者:行者123 更新时间:2023-11-30 06:13:40 25 4
gpt4 key购买 nike

我试图获取它,以便当用户输入吉他型号时,它将从我的 ArrayList 返回该特定型号的所有详细信息。但是,当我运行该程序时,没有收到任何错误,只是没有结果。我不知道该怎么做,任何帮助将不胜感激。

附注在我的开关案例中,我一直尝试多种不同的方法来获得结果,但没有运气,所以如果它到处都是,请忽略。

package guitarsrentalmanagementsystem;

import StockManagement.Guitar;
import java.util.ArrayList;
import java.util.Scanner;

public class GuitarsRentalManagementSystem {

public static void main(String[] args) {
// TODO code application logic here
ArrayList<Guitar> data = new ArrayList<>();
data.add(new Guitar(100, 150.99, "Gibson", "Les Paul",
"Acoustic", "INDIAN_ROSEWOOD", "MAHOGANY", 1995, 8.99, "Red",
true));
data.add(new Guitar(101, 180.00, "Fender", "Stratocaster",
"Electric", "BRAZILIAN_ROSEWOOD", "MAHOGANY", 2001, 10.99, "Brown",
true));
data.add(new Guitar(102, 110.50, "Martin", "Noisemaker",
"Acoustic", "INDIAN_ROSEWOOD", "BRAZILIAN_ROSEWOOD", 2005, 5.99, "Black",
true));

for(Guitar g : data){
g.printDetail();
}

//Show data from the adobe arraylist
System.out.println("Valid search parameters are as follows :");
System.out.println("1. Model, eg Les Paul");
System.out.println("2. Serial number, eg 101");
System.out.println("3. Year of manufacture, eg 2001");

Scanner input = new Scanner(System.in);
System.out.println("Please select how you would like to search");
int search = input.nextInt();
for(Guitar g : data){
switch(search){
case 1:
System.out.println("You have chosen to search by model");
System.out.println("Valid models are Les Paul, Stratocaster or Noisemaker");
System.out.println("Please enter a model to search");
Scanner inputO1 = new Scanner(System.in);
String modelSearch = input.next();
if("Les Paul".equals(modelSearch)){
g.printDetail();
}
break;

case 2:
System.out.println("You have chosen to search by Serial number");
System.out.println("valid serial numbers are between 100 and 105");
System.out.println("Please enter a serial number to search");
Scanner inputO2 = new Scanner(System.in);
int serialSearch = input.nextInt();
System.out.println(g.getSerialNumber());
break;

case 3:
System.out.println("You have chosen to search by year of manufacture");
System.out.println("Valid manufacture dates are 1995, 2001 and 2005");
System.out.println("Please enter a manufacture year to search");
Scanner input03 = new Scanner(System.in);
int yearSearch = input.nextInt();
System.out.println(g.getYearOfManufacture());

break;
}
break;
}
}
}

最佳答案

您读取输入的方式存在问题:当您使用扫描仪对象并对其调用 nextInt() 时。它只读取数字。类似地,当您调用 next 时,它只读取一个字符串值,即

如果您输入了“Les Paul”作为输入,扫描仪对象的 next() 方法将只准备 Les,因此它与您的条件不匹配。

因此,一旦读取整数,就调用 nextLine() 方法,因此光标移动到下一行,现在通过再次调用 nextLine() 方法来读取下一行的输入

 case 1:
System.out.println("You have chosen to search by model");
System.out.println("Valid models are Les Paul, Stratocaster or Noisemaker");
System.out.println("Please enter a model to search");
Scanner inputO1 = new Scanner(System.in);
input.nextLine();
String modelSearch = input.nextLine();
if ("Les Paul".equals(modelSearch)) {
g.printDetail();
}
break;

如果用户输入模型为“Les Paul”,则上述代码将起作用。

关于java - 无法让我的代码从 ArrayList 返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com