我正在尝试设计一个读取美元金额的购物车,然后将这些值打印回来。但是,我的while循环不会终止,我的arraylist会存储错误的值。
/**
* This method is the shopping cart
*/
public static void shoppingCart()
{
// create scanner objects
Scanner textReader = new Scanner(System.in);
Scanner numberReader = new Scanner(System.in);
// declare variables
int counter = 0;
// create arraylist object
ArrayList<Double> cartItems = new ArrayList<Double>();
// create decimal format object
DecimalFormat dollarformatter = new DecimalFormat("$#0.00");
// print out the first prompt
System.out.print("Would you like to input item/s - y/n: ");
String response = textReader.nextLine();
System.out.println();
// create while loop to restrict responses to single characters
while ((!response.equalsIgnoreCase("y")) && (!response.equalsIgnoreCase("n")))
{
System.out.print("Sorry - we need a y/n: ");
response = textReader.nextLine();
System.out.println();
}
// create while loop for positive response
while ((response.equalsIgnoreCase("y")))
{
System.out.print("Please enter an item price, or -1 to exit: $");
double values = numberReader.nextDouble();
cartItems.add(values);
if ((values > (-1)))
{
System.out.print("Please enter another item price, or -1 to exit: $");
values = numberReader.nextDouble();
cartItems.add(values);
}
else if ((values <= (-1)))
{
System.out.println();
System.out.println("********** Here are your items **********");
System.out.println();
for (counter = 0; counter < cartItems.size(); counter++)
{
System.out.println("Item #" + (counter + 1) + ": " + cartItems.get(counter));
}
}
}
System.out.println();
System.out.println("********** Thank you for using the shopping cart **********");
}
结果应如下所示:
但这是我的输出:
更新了代码;循环终止问题似乎已经解决,但“-1”没有触发退出,它仍然被添加到arrayList。
while ((response.equalsIgnoreCase("y"))) {
System.out.print("Please enter an item price, or -1 to exit: $");
double values = numberReader.nextDouble();
cartItems.add(values);
while ((values != (-1))) {
System.out.print("Please enter another item price, or -1 to exit: $");
values = numberReader.nextDouble();
cartItems.add(values);
}
System.out.println();
System.out.println("********** Here are your items **********");
System.out.println();
for (counter = 0; counter < cartItems.size(); counter++) {
System.out.println("Item #" + (counter + 1) + ": " + cartItems.get(counter));
}
break;
}
答案 0 :(得分:1)
问题1:cartItems.add(values)在if语句之前发生,意味着-1仍然被添加到购物车。换句话说,您甚至在测试之前将值添加到购物车是否低于零。
问题2:你的其他人如果执行了,但由于它处于while循环中并且你的响应没有改变,它会再次提示用户,因为while循环条件仍然是真的。我建议在else if语句的末尾添加一个break语句。但我的代码消除了对此的需求。
这就是我要做的事情:
if((response.equalsIgnoreCase("y")))
{
System.out.print("Please enter an item price, or -1 to exit: $");
double values = numberReader.nextDouble();
while ((values > (-1)))
{
cartItems.add(values);
System.out.print("Please enter another item price, or -1 to exit: $");
values = numberReader.nextDouble();
}
}
System.out.println();
System.out.println("********** Here are your items **********");
System.out.println();
for (counter = 0; counter < cartItems.size(); counter++){
System.out.println("Item #" + (counter + 1) + ": " + cartItems.get(counter));
}
System.out.println();
System.out.println("********** Thank you for using the shopping cart **********");
基本上我所做的就是意识到所有你需要的是一种方法来进行一个while循环,这样你就不必在知道它低于0之前过早地添加到你的购物车中,就像你在之前的代码中所做的那样。我建议调查栅栏问题。栅栏柱的形式为| - | - |所以栅栏贴是你的提示,电线正在添加值。因此,我的方式更好,因为我改变了顺序。它就这么简单。
答案 1 :(得分:0)
请记住,您在多个不同条件下都有for循环。它将继续运行,直到while循环完成。