我正在尝试设计一个购物车,该购物车读取美元金额,然后将这些值打印回来。但是,我的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;
}
问题1:cartItems.add(values)发生在if语句之前,这意味着-1仍会添加到购物车中。换句话说,您要在购物车中添加值,然后再测试它是否小于零。
问题2:您的else if执行,但是由于它处于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。我建议调查栅栏问题。围栏柱的形式为|-|-| 等等。因此,栅栏柱是您的提示,并且电线正在添加值。因此,我的方法更好,因为我切换了顺序。就这么简单。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句