Java 我的高级for循环有问题
我想让我的程序搜索一个对象id(一次),然后用我的infull()方法更改数量,但它要求我在工作前多次键入id,我如何解决这个问题Java 我的高级for循环有问题,java,for-loop,Java,For Loop,我想让我的程序搜索一个对象id(一次),然后用我的infull()方法更改数量,但它要求我在工作前多次键入id,我如何解决这个问题 import java.util.*; class TestPart { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Part> parts = new ArrayList<Pa
import java.util.*;
class TestPart {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Part> parts = new ArrayList<Part>();
parts.add(new Part("p122", "Chain", 48, 12.50));
parts.add(new Part("p123", "Chain Guard", 73, 22.00));
parts.add(new Part("p124", "Crank", 400, 11.50));
parts.add(new Part("p125", "Pedal", 38, 6.50));
parts.add(new Part("p126", "Handlebar", 123, 9.50));
System.out.println("part before stock level change - start");
System.out.println(Part.toString(parts));
System.out.println("To replenish type 'R' or to supply type 'S'");
String choice = sc.nextLine();
for(int i = 0; i<1; i++) {
if (choice.equals("R")){
System.out.println("Please enter ID of part you would like "
+ "to replenish");
for (Part part: parts)
{
if(part!=null && sc.nextLine().equals(part.getID())){
System.out.println("Please enter replenish quantity");
part.replenish(sc.nextInt());
}
}
} else {
System.out.println("You did not type 'R' or 'S'");
}
}
/*System.out.println("Please enter ID");
//parts.get(1).replenish(sc.nextInt());
for (Part part: parts) {
if(part!=null && sc.nextLine().equals(part.getID())){
System.out.println("Please enter replenish quantity");
part.replenish(sc.nextInt());
}
}*/
System.out.println("-------------------------------------");
System.out.println("part after stock level change - start");
System.out.println(Part.toString(parts));
System.out.println("end");
sc.close();
}
}
import java.util.*;
类测试部件{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
列表部件=新的ArrayList();
增加(新零件(“p122”,“链”,48,12.50));
零件。增加(新零件(“p123”,“链条防护装置”,73,22.00));
增加(新零件(“p124”,“曲柄”,400,11.50));
增加(新零件(“p125”、“踏板”、38、6.50));
零件。添加(新零件(“p126”,“把手”,123,9.50));
System.out.println(“库存水平变化前的零件-启动”);
System.out.println(部分toString(部分));
System.out.println(“补充“R”型或供应“S”型”);
字符串选择=sc.nextLine();
对于(int i=0;i来说,基本上把这部分代码放在下面是个坏主意:
sc.nextLine().equals(part.getID())
因为sc.nextLine()
试试这个:
if (choice.equals("R")){
System.out.println("Please enter ID of part you would like "
+ "to replenish");
String input = sc.nextLine();
for (Part part: parts)
{
if(part!=null && input.equals(part.getID())){
System.out.println("Please enter replenish quantity");
part.replenish(sc.nextInt());
}
}
}
您将被询问一次,并且必须将其输入一次。然后,它将存储在字符串变量中。每当您再次需要它时,请使用该变量。基本上,输入这部分代码是个坏主意:
sc.nextLine().equals(part.getID())
因为sc.nextLine()
试试这个:
if (choice.equals("R")){
System.out.println("Please enter ID of part you would like "
+ "to replenish");
String input = sc.nextLine();
for (Part part: parts)
{
if(part!=null && input.equals(part.getID())){
System.out.println("Please enter replenish quantity");
part.replenish(sc.nextInt());
}
}
}
您将被询问一次,并且必须将其输入一次。然后将其存储在字符串变量中。每当您再次需要它时,请使用该变量。哦,是的,我明白了。与我对选择变量所做的方式相同?谢谢!@danielb是的,没错,我明白了。与我对选择变量所做的方式相同?谢谢!@danielb yes、 关于代码质量的exactlySide注释:你在一个方法中做的事情太多了。最重要的是:不要使用静态的主要方法进行测试;而是要学习如何编写单元测试。相信我,进入单元测试所需的相对较少的时间会很快得到回报。感谢@Jägermeister!我将非常感谢你的提示研究如何为将来编写单元测试。目前,我的这部分作业(针对uni)一种方法就可以了。代码质量旁注:你在一个方法中做的事情太多了。最重要的是:不要使用静态的主方法进行测试,而是学习如何编写单元测试。相信我,进入单元测试所需的相对较少的时间会很快得到回报。谢谢@Jägermeister!我会有效地研究如何为将来编写单元测试。目前,对于我的这部分作业(对于uni),一种方法就可以了。