【问题标题】:Trouble with breaking out of a while loop when inputting a Quantity输入数量时中断while循环的问题
【发布时间】:2015-04-06 09:34:49
【问题描述】:

我有一个问题。我基本上完成了我的程序,除了我创建的 while 循环的最后一个中断。如果我输入 -1 以跳出我的第一个输出问题“输入产品编号 (1-5) 或 -1 以退出”,它就会毫无问题地跳出。如果我在第二个问题“输入数量或 -1 退出”中输入 -1,它只会循环回到下一个产品编号请求。

我已尝试查找一些有关打破 while 循环的其他信息,并尝试将相同的逻辑应用于我现有的代码。不幸的是,我仍然遇到同样的问题。看起来这个特定的请求不是一个重复的问题,所以我觉得问它是安全的。我到底做错了什么,因为经过大约一个小时的努力想弄清楚这一点,我简直要发疯了。任何方向将不胜感激。

谢谢,

进步

导入 java.util.Scanner;

公共类 ModuleFour {

public static void main(String[] args) {

    int pNumber = 0; //declaring integer for Product Number
    int pQuantity= 0; //declaring integer quantity of Product
    double totalcost = 0; //declaring double for Total Cost of combined Product
    double cost = 0; //declaring double for cost of each Product
        Scanner productTotal = new Scanner(System.in); //create scanner productTotal
        pNumber = 0;
        pQuantity = 0;
    while (pNumber >= 0 && pNumber <=5) //set condition of pNumber for while loop
    {
        System.out.println("Enter Product Number (1-5) or -1 to Quit"); //print out asking for input
        pQuantity = productTotal.nextInt(); //
    switch(pQuantity) { //begin switch statement
    case 1: //start of cases, all are the same. 
        System.out.println("Product " + (pNumber+1));
        System.out.println("Enter Quantity or -1 to Quit");
        pQuantity = productTotal.nextInt(); 
        cost = 2.98; //declare cost of case 1. Same method for each case. 
        totalcost = totalcost + cost*pQuantity; //setting totalcost algorithm.
        System.out.println("Current total cost: " + totalcost); //print out the current totalcost.
        pNumber++; //increase value of pNumber by 1. Same for each case. 
    break; //break out of Case 1. Same for each case.
    case 2:
        System.out.println("Product " + (pNumber+1));
        System.out.println("Enter Quantity or -1 to Quit");
        pQuantity = productTotal.nextInt();
        cost = 4.50;
        totalcost = totalcost + cost*pQuantity;
        System.out.println("Current total cost: " + totalcost);
        pNumber++;
    break;
    case 3:

        System.out.println("Product " + (pNumber+1));
        System.out.println("Enter Quantity or -1 to Quit");
        pQuantity = productTotal.nextInt();
        cost = 9.98;
        totalcost = totalcost + cost*pQuantity;
        System.out.println("Current total cost: " + totalcost);
        pNumber++;
    break;
    case 4:
        System.out.println("Product " + (pNumber+1));
        System.out.println("Enter Quantity or -1 to Quit");
        pQuantity = productTotal.nextInt();
        cost = 4.49;
        totalcost = totalcost + cost*pQuantity;
        System.out.println("Current total cost: " + totalcost);
        pNumber++;
    break;
    case 5:
        System.out.println("Product " + (pNumber+1));
        System.out.println("Enter Quantity or -1 to Quit");
        pQuantity = productTotal.nextInt();
        cost = 6.87;
        totalcost = totalcost + (cost*pQuantity);
        System.out.println("Current total cost: " + totalcost);
        pNumber++;
    case -1: //declare case for -1 to break out of while loop
        ****pNumber = -1; //set condition for pNumber to break out of loop with input of -1.
        pQuantity = -1; //set condition for pQuantity to break out of loop with input of -1.****
    break; //when condition is met (-1 as input) break out of while loop.
    }
        System.out.println("Total cost-->" +totalcost); //print out the final total cost
    }

}

}

【问题讨论】:

    标签: java while-loop


    【解决方案1】:

    switch 语句中不需要大小写 -1。相反,您可以将 pQuantity > 1 的条件添加到 while 条件中,如下所示:

    while ((pNumber >= 0 && pNumber <=5) || ( pQuantity >1)) 
    

    现在,如果你在第一个问题中输入 -1,它退出程序不是因为最后一个 case -1 条件,而是因为 while 循环条件。但是当你对第二个问题(产品数量)输入-1时,它会转到相应的case条件,完成并跳出switch语句,并返回到while循环。它不会进行 case -1 条件检查。

    【讨论】:

    • 感谢您的快速回复!因此,我将您的建议添加到 while 条件中。当我在第一个问题中输入 -1 时,它会像以前一样中断。如果我在第二个问题中输入 -1(删除了我的 Case-1 的那部分(并且我尝试将 case-1 完全注释掉),我仍然需要输入 -1 两次才能完全跳出 while 循环。我感觉好像我还缺少什么。
    • 对不起,我在之前的回答中犯了一个错误。实际答案应该是 while ((pNumber &gt;= 0 &amp;&amp; pNumber &lt;=5) &amp;&amp; ( pQuantity != -1 )) 而不是之前的答案,因为只有当 pNumber 介于 0 和 5 之间且 pQuantity != -1 时才想继续循环
    • 其实我也想通了!我在玩代码并意识到,你知道吗,我在循环条件中没有 pQuantity 本身。最终我的任务得到了 50/50,我很满意。我只是希望我们的教授能留下注释或一些批评告诉我如何改进我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2021-10-14
    • 2020-07-07
    • 2011-06-19
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多