Java Getter方法在增强循环中不起作用

Java Getter方法在增强循环中不起作用,java,if-statement,for-loop,getter,Java,If Statement,For Loop,Getter,我正在制作一个增强的for循环,以对零件数组列表中的零件对象进行排序。但是当增强的for循环到达if语句时,它什么也不做(除了Damper),并且没有错误,所以我不知道为什么它不工作列表是包含对象的数组列表的名称 for(int z=0; z<20; z++){ displayMenu(); int selection = sc.nextInt(); if(selection == 0){

我正在制作一个增强的for循环,以对零件数组列表中的零件对象进行排序。但是当增强的for循环到达if语句时,它什么也不做(除了
Damper
),并且没有错误,所以我不知道为什么它不工作<代码>列表是包含对象的数组列表的名称

    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
为了向大家展示这一点 正在工作


此外,它仅适用于阻尼器

首先,您需要添加

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");
        }
    }

您的问题是,您正在使用nextLine,然后是nextLine,然后是nextLine。新台词把你搞砸了

for(int z=0; z<20; z++){
    displayMenu();
        int selection = Integer.parseInt(sc.nextLine());
        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 = Integer.parseInt(sc.nextLine());
                    y.replenish(qty);
                    System.out.println("Complete");
                }
            }
        } else if

for(int z=0;zNo,因为它还没有到达那里。它从不打印“请输入补充数量”。除了阻尼器。当我把阻尼器放进去时,它工作得很好,我想他以前是说!这样做只会跳过所有内容,并循环回displayMenu()@dansey我更新了它。你每次调用sc.nextLine()时都需要添加sc.nextLine()@dansey你能用例子展示一个可运行的代码吗?
for(int z=0; z<20; z++){
    displayMenu();
        int selection = Integer.parseInt(sc.nextLine());
        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 = Integer.parseInt(sc.nextLine());
                    y.replenish(qty);
                    System.out.println("Complete");
                }
            }
        } else if