Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayList的Java While循环赢得';不要终止_Java_If Statement_Arraylist_While Loop - Fatal编程技术网

ArrayList的Java While循环赢得';不要终止

ArrayList的Java While循环赢得';不要终止,java,if-statement,arraylist,while-loop,Java,If Statement,Arraylist,While Loop,我正在尝试设计一个购物车,它可以读取美元金额,然后将这些金额打印出来。然而,我的while循环不会终止,我的arraylist正在存储错误的值 /** * This method is the shopping cart */ public static void shoppingCart() { // create scanner objects Scanner textReader = new Scanner(System.in); Scanner number

我正在尝试设计一个购物车,它可以读取美元金额,然后将这些金额打印出来。然而,我的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 **********");
}
/**
*这种方法就是购物车
*/
公共静态void shoppingCart()
{
//创建扫描仪对象
扫描仪文本阅读器=新扫描仪(System.in);
扫描仪编号Reader=新扫描仪(System.in);
//声明变量
int计数器=0;
//创建arraylist对象
ArrayList cartItems=新的ArrayList();
//创建十进制格式对象
DecimalFormat dollarformatter=新的DecimalFormat($#0.00”);
//打印出第一个提示
System.out.print(“是否要输入项目/s-y/n:”);
String response=textReader.nextLine();
System.out.println();
//创建while循环以将响应限制为单个字符
而((!response.equalsIgnoreCase(“y”)&(!response.equalsIgnoreCase(“n”))
{
System.out.print(“对不起,我们需要一个y/n:”);
response=textReader.nextLine();
System.out.println();
}
//为积极响应创建while循环
while((response.equalsIgnoreCase(“y”))
{
System.out.print(“请输入商品价格,或-1退出:$”;
双值=numberReader.nextDouble();
cartItems.add(值);
如果((值>(-1)))
{
System.out.print(“请输入其他项目价格,或-1退出:$”;
值=numberReader.nextDouble();
cartItems.add(值);
}

else if((值)请记住,for循环在多个不同的条件下运行。它将一直运行,直到循环完成。

问题1:cartItems.add(值)发生在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 **********");
if((response.equalsIgnoreCase(“y”))
{
System.out.print(“请输入商品价格,或-1退出:$”;
双值=numberReader.nextDouble();
而((值>-1)))
{
cartItems.add(值);
System.out.print(“请输入其他项目价格,或-1退出:$”;
值=numberReader.nextDouble();
}
}
System.out.println();
System.out.println(“*************这是您的项目**********”;
System.out.println();
用于(计数器=0;计数器

基本上,我所做的是认识到,你所需要的只是一种方法,以有一个while循环,这样你就不必过早地添加到你的购物车之前,你知道它是低于0,因为你在你的早期代码。我建议调查一个栅栏柱的问题。栅栏柱的形式是|-|-|-|-|等。所以栅栏柱是你的提示,电线是额外的因此,我的方法是更好的,因为我切换了顺序。它是如此简单。

你的if语句仍然在while循环中,所以提示将被再次打印。也许你应该考虑放一个中断语句?你也可以在你的if和OR语句之前把这个值添加到手推车中,所以它仍然会加上-1。能够解释为什么它不会将“-1”读作“退出”?即使我输入“-1”,代码继续运行。当条件为true时,while循环不断重复。输入if-else语句,但它返回while循环的顶部。我想我已修复了循环终止问题,但仍在添加-1。是否放置cartItems.add(值)在if语句中,并将其从if语句之前删除?实际上,我完全删除了if/else语句。相反,我使用了while语句。我在上面的原始帖子中更新了代码。您是否建议我使用if/else语句?我认为while语句可能会澄清问题。我认为早期的代码很好。您已经仍然两次添加(值)。删除旧代码中if语句之前的一个。在新代码中删除嵌套while循环中的该语句。是的,没问题,我很高兴它有所帮助。可能您的类还没有开始添加,我几乎每周都会遇到栅栏柱问题。感谢您的支持:)
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 **********");