ArrayList 的 Java While 循环不会终止

标签 java if-statement arraylist while-loop

我正在尝试设计一个购物车,它可以读取美元金额,然后将这些值打印回来。但是,我的 while 循环不会终止,并且我的数组列表存储了错误的值。

 /**
 * 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 **********");
}

结果应如下所示:

Correct output

但这是我的输出:

my output

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

更新了代码;循环终止问题似乎已解决,但“-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 循环条件仍然为 true。我建议在 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 之前提前添加到购物车。我建议调查一下栅栏柱问题。栅栏柱的形式为 |-|-|等等。所以栅栏柱是你的提示,电线正在添加值。因此,我的方法更好,因为我改变了顺序。就这么简单。

关于ArrayList 的 Java While 循环不会终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604607/

相关文章:

java - 向可扩展 ListView 添加按钮

java - Apache Tomcat 9 未在 Eclipse 服务器运行时环境中显示

java - 按两个字段对 Arraylist 进行排序 (Java)

java - 仅在 oop 中将方法的范围限制到另一个类

java - 如何在 Linux 中更新 memcached 服务器

java - 我需要一些帮助来制作一个打印 1-100 的表格

c - 检测 scanf 读取的值的变化

jquery - 如果条件与 css : setting a border and removing when clicked consequently

java - 如何在Java中创建文件列表?

android - 如何连接删除按钮以删除 ListView 中的一个项目?