【问题标题】:Getter method not working in enhanced loopGetter 方法在增强循环中不起作用
【发布时间】:2015-10-27 11:21:08
【问题描述】:

我正在制作一个增强的 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' 工作中

此外,它仅适用于阻尼器

【问题讨论】:

    标签: java if-statement for-loop getter


    【解决方案1】:

    首先,你需要添加

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

    【讨论】:

    • 不,因为它甚至没有到达那里。它从不打印“请输入补充数量”。除了阻尼器。当我把阻尼器放进去时,它工作得很好
    • 我想他之前的意思!
    • 这样做只会跳过所有内容并循环回 displayMenu()
    • @dansey 我更新了它。每次调用 sc.nextInt() 时都需要添加 sc.nextLine();
    • @dansey 你能用例子展示一个可运行的代码吗?
    【解决方案2】:

    您的问题是您使用的是 nextInt,然后是 nextLine,然后是 nextInt。新行 char 把你搞砸了。

    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
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多