如何让 Java 忽略 ArrayList 中的负值?
How to get Java to ignore negative values in an ArrayList?
我正在创建一个允许根据用户输入打印购物清单的程序。
我正在尝试添加用户验证,这样当您为产品输入负价格时,它会要求用户再次输入一个值,因为您不能添加负价格。
虽然控制台确实可以工作,并在您给它一个无效值时要求用户输入另一个值,但它仍然会将负值添加到数组中,随后在您支付时将负值添加到总金额中.
有没有办法让我的代码忽略负值而不将其添加到数组列表中?我才刚刚开始,所以它会有很大帮助。
While 循环
// while loop until user enters the product
while (true) {
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = sc.nextDouble();
// if user enters negative price it'll be rejected
if (price < 0)
System.out.print("Price cannot be a negative value, please enter price again: ");
sc.nextLine();
// adds product and price to array lists
products.add(product);
prices.add(price);
}
正在将总数添加到数组列表中:
//method to get the total of the shopping list
public static double getTotalList(ArrayList<Double> prices) {
// variable that stores the total
double total = 0;
// add to the total
for (double i: prices)
total += i;
// return the total
return total;
}
如果用户再次提供负值你该怎么办你想让他们租用直到有效然后你需要把它放在while循环中
如果(价格 < 0)
System.out.print("价格不能为负值,请重新输入价格:");
直到用户输入有效值,或者您可以使用 else 语句忽略该值
或者您在 for each 循环中使用 if 语句只添加正数
或使用流,但由于您刚刚开始,所以您可能还没有到达那部分
这应该可以解决问题:
while (true)
{
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = sc.nextDouble();
// if user enters negative price it'll be rejected
// This is the change, we have a loop which continuously checks if the price is negative, if it is, the user will be asked again
while (price < 0)
{
System.out.print("Price cannot be a negative value, please enter price again: ");
price = sc.nextDouble();
}
// adds product and price to array lists
products.add(product);
prices.add(price);
}
如果您需要拒绝负价格,您应该按照@Renis1235 的回答中的建议在循环中实现 price
的读取,或者您可以使用递归方法读取价格,直到输入有效值:
static double readValidPrice(Scanner sc) {
double price = sc.nextDouble();
sc.nextLine(); // skip line break
if (price < 0) {
System.out.print("Price cannot be a negative value, please enter price again: ");
return readValidPrice(sc); // recursive call
}
return price;
}
然后从主循环中调用这个方法:
while (true) {
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = readValidPrice(sc); // valid value is guaranteed to be read
// adds product and price to array lists
products.add(product);
prices.add(price);
}
此方法应在 prices
列表中提供有效值,因此总和应该是正确的。
但是,要在方法 getTotalList
中强制过滤掉负值,您也可以添加价格检查。使用Java Stream API的例子如下:
public static double getTotalList(List<Double> prices) {
return prices.stream()
.filter(price -> price > 0.0)
.mapToDouble(Double::doubleValue) // DoubleStream
.sum();
}
不是你想忽略负输入,而是你只想要正输入。
创建一个方法来处理这个问题:
double getPrice(Scanner sc, String description) {
while (true) {
System.out.print(description + " : ");
double price = sc.nextDouble();
sc.nextLine();
if (price >= 0)
return price;
System.out.print("Price cannot be a negative value");
}
}
然后使用它:
double price = getPrice(sc, "Enter the price for " + product;
这种方法可以处理任意数量的无效价格,并使主要代码更易于阅读。
我正在创建一个允许根据用户输入打印购物清单的程序。
我正在尝试添加用户验证,这样当您为产品输入负价格时,它会要求用户再次输入一个值,因为您不能添加负价格。
虽然控制台确实可以工作,并在您给它一个无效值时要求用户输入另一个值,但它仍然会将负值添加到数组中,随后在您支付时将负值添加到总金额中.
有没有办法让我的代码忽略负值而不将其添加到数组列表中?我才刚刚开始,所以它会有很大帮助。
While 循环
// while loop until user enters the product
while (true) {
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = sc.nextDouble();
// if user enters negative price it'll be rejected
if (price < 0)
System.out.print("Price cannot be a negative value, please enter price again: ");
sc.nextLine();
// adds product and price to array lists
products.add(product);
prices.add(price);
}
正在将总数添加到数组列表中:
//method to get the total of the shopping list
public static double getTotalList(ArrayList<Double> prices) {
// variable that stores the total
double total = 0;
// add to the total
for (double i: prices)
total += i;
// return the total
return total;
}
如果用户再次提供负值你该怎么办你想让他们租用直到有效然后你需要把它放在while循环中 如果(价格 < 0) System.out.print("价格不能为负值,请重新输入价格:");
直到用户输入有效值,或者您可以使用 else 语句忽略该值
或者您在 for each 循环中使用 if 语句只添加正数
或使用流,但由于您刚刚开始,所以您可能还没有到达那部分
这应该可以解决问题:
while (true)
{
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = sc.nextDouble();
// if user enters negative price it'll be rejected
// This is the change, we have a loop which continuously checks if the price is negative, if it is, the user will be asked again
while (price < 0)
{
System.out.print("Price cannot be a negative value, please enter price again: ");
price = sc.nextDouble();
}
// adds product and price to array lists
products.add(product);
prices.add(price);
}
如果您需要拒绝负价格,您应该按照@Renis1235 的回答中的建议在循环中实现 price
的读取,或者您可以使用递归方法读取价格,直到输入有效值:
static double readValidPrice(Scanner sc) {
double price = sc.nextDouble();
sc.nextLine(); // skip line break
if (price < 0) {
System.out.print("Price cannot be a negative value, please enter price again: ");
return readValidPrice(sc); // recursive call
}
return price;
}
然后从主循环中调用这个方法:
while (true) {
// asks user for product input
System.out.print("Please enter product name or enter 'x' to finish: ");
product = sc.nextLine();
// terminates the loop if the user enters x
if (product.equalsIgnoreCase("x"))
break;
// asks user for the price of the product
System.out.print("Enter the price for " + product + " : ");
price = readValidPrice(sc); // valid value is guaranteed to be read
// adds product and price to array lists
products.add(product);
prices.add(price);
}
此方法应在 prices
列表中提供有效值,因此总和应该是正确的。
但是,要在方法 getTotalList
中强制过滤掉负值,您也可以添加价格检查。使用Java Stream API的例子如下:
public static double getTotalList(List<Double> prices) {
return prices.stream()
.filter(price -> price > 0.0)
.mapToDouble(Double::doubleValue) // DoubleStream
.sum();
}
不是你想忽略负输入,而是你只想要正输入。
创建一个方法来处理这个问题:
double getPrice(Scanner sc, String description) {
while (true) {
System.out.print(description + " : ");
double price = sc.nextDouble();
sc.nextLine();
if (price >= 0)
return price;
System.out.print("Price cannot be a negative value");
}
}
然后使用它:
double price = getPrice(sc, "Enter the price for " + product;
这种方法可以处理任意数量的无效价格,并使主要代码更易于阅读。