ArrayList的Java While循环不会终止

iMagicMango

我正在尝试设计一个购物车,该购物车读取美元金额,然后将这些值打印回来。但是,我的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 **********");
}

结果应如下所示:

正确的输出

但这是我的输出:

我的输出

  • while循环不会终止,而是返回到第一个提示:“请输入商品价格,或-1退出:”
  • 程序将“ -1”作为数组列表的一部分。“ -1”值应该用作“ no”,并终止向arrayList添加更多元素的操作,但是在我的代码中,它被吸收到arrayList中。我尝试将“ -1”转换为字符串以使程序“忽略”它,但是它不起作用。
  • 程序列出了最后一个项目(在我的输出中是#3)之后,应该询问用户是否要删除一个项目。我还没弄清楚为什么我的while循环拒绝终止以及为什么“ -1”一直被包含在我的arraylist中,所以我对此还很困惑。对此的任何帮助都将受到极大的赞赏,因为我已经考虑了这一天,没有运气。

更新的代码;循环终止问题似乎已解决,但“ -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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章