为什么不能在此循环中从另一个类调用方法?

时间:2019-02-03 11:09:56

标签: java list class methods

我正在尝试在Catalog类中创建一个过滤器,稍后将打印出价格高于用户输入的最低价格的“零件”。但是,我无法从Catalog类调用getNumPrice()(在Part类中),我不确定为什么吗?我该如何解决这个问题,我在做什么错了?

   //The following is in the Part class

 public double getNumPrice(){
        return this.price;
    }

//The following is in the Catalogue class

 private double readMinPrice(){
        System.out.println("Enter minimum price ('-1' for no filtering): ");
        return In.nextDouble();
    }

 private void filter(){
        String type =readTypeFilter();
        double minPrice = readMinPrice();

       if ( type== "all" && minPrice==-1)
            showParts();
        else if (type=="all" && minPrice >= 0)

            for(int i=0; i<= parts.size();i++)
                if (part.getNumPrice() >= minPrice)
                    System.out.println( i+1 + "." +  parts.get(i));
}

2 个答案:

答案 0 :(得分:0)

您有一个parts集合,并且正在循环中调用part.getNumPrice()

我没有在您的代码中声明任何一个,但是我假设parts是一个字段,并且您正在尝试在循环中执行类似parts.get(i).getNumPrice()的操作。

答案 1 :(得分:0)

您需要在循环中使用索引部分。

for(int i=0; i<= parts.size();i++)
   if (parts.get(i).getNumPrice() >= minPrice)
相关问题