我正在制作一个增强的for循环,以对我的零件阵列列表中的零件对象进行排序。但是,当增强的for循环进入if语句时,它什么也不做(除外Damper
),并且没有错误,所以我不知道为什么它不起作用。thelist
是我的包含对象的数组列表的名称。
for(int z=0; z<20; z++){
displayMenu();
int selection = sc.nextInt();
if(selection == 0){
System.out.println("You have exited the system");
} else if(selection == 1){
System.out.println("Choose stock name to replenish");
for(Part y: thelist){
String nS = sc.nextLine();
System.out.print(nS);
if(nS.equals(y.getName())){
System.out.println("Please enter replenish quantity");
int qty = sc.nextInt();
y.replenish(qty);
System.out.println("Complete");
}
}
} else if
我的物件
Part part0 = new Part("p100", "Spring", 43, 120);
Part part1 = new Part("p101", "Damper", 72, 150);
Part part2 = new Part("p102", "Lower CA", 38, 80);
Part part3 = new Part("p103", "Upper CA", 26, 70);
只是为了证明最多'String nS'在工作
此外,它仅适用于阻尼器
首先,您需要添加
sc.nextLine();
后
int qty = sc.nextInt();
然后添加相同的
int selection = sc.nextInt();
消耗换行符。
其次,如果您要搜索nS,为什么将其放在for循环中?将其放在for循环外,然后搜索。
String nS = sc.nextLine();
System.out.print(nS);
for (Part y : thelist) {
if (nS.equals(y.getName())) {
System.out.println("Please enter replenish quantity");
int qty = sc.nextInt();
sc.nextLine();
y.replenish(qty);
System.out.println("Complete");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句