gpt4 book ai didi

java - 想要根据产品价格过滤产品并打印过滤后的产品

转载 作者:行者123 更新时间:2023-12-01 10:18:45 25 4
gpt4 key购买 nike

我正在尝试通过获取产品列表并对其进行迭代来过滤产品..

public List<Products> filterByPrice(int min,int max){   

List<Products> listofproducts = products.retrieveProducts();
System.out.println(listofproducts +" size: " +listofproducts.size());

for (Products productsVar : listofproducts) {
if(productsVar.getPrice()>= min && productsVar.getPrice()<= max){
return listofproducts; //here how do i print the reduced listOfProducts
}
}
return null;
}

可能很简单,但不知道如何从列表中打印出减少的过滤产品

谢谢

最佳答案

您正在打印并返回整个列表,请尝试以下操作:

List<Products> listofproducts = New List<Products>();
//System.out.println(listofproducts +" size: " +listofproducts.size());

for (Products productsVar : products.retrieveProducts()){
if(productsVar.getPrice()>= min && productsVar.getPrice()<= max){
listofproducts.add(productsVar);
}
return listofproducts; // may return blank list if no products fall between min and max price range

关于java - 想要根据产品价格过滤产品并打印过滤后的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35754276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com