为什么我不能调用从另一个类在这个循环的方法?

问题描述 投票:-1回答:2

我试图在目录类,稍后会打印出“部件”的具有比用户输入的最低价格更高的价格过滤器。但是我无法从目录类调用getNumPrice()(这是在部件类),我不知道为什么?我怎样才能解决这个问题和我究竟做错了什么?

   //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));
}
java list class methods
2个回答
0
投票

你有一个parts收集和你在你的循环中调用part.getNumPrice()

我没有看到你的代码,通过声明,但我假设parts是一个领域,你正在试图做类似的循环parts.get(i).getNumPrice()


0
投票

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

for(int i=0; i<= parts.size();i++)
   if (parts.get(i).getNumPrice() >= minPrice)
© www.soinside.com 2019 - 2024. All rights reserved.