输入数量时出现 while 循环中断问题

Trouble with breaking out of a while loop when inputting a Quantity

我有一个问题。我基本上完成了我的程序,除了我创建的 while 循环的最后一次中断。如果我输入 -1 来跳出我的第一个输出问题 "Enter Product Number (1-5) or -1 to Quit",它会毫无问题地跳出。如果我在第二个问题 "Enter Quantity or -1 to Quit" 中输入 -1,它只会循环回到下一个产品编号请求。

我已经尝试查找一些关于打破 while 循环的附加信息,并尝试将相同的逻辑应用于我现有的代码。不幸的是,我仍然遇到同样的问题。看来这个特定请求不是重复问题,所以我可以放心提问。我到底做错了什么,因为在尝试解决这个问题大约一个小时后,我简直要疯了。任何方向将不胜感激。

谢谢,

程序

导入java.util.Scanner;

public class 模块四 {

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
    }

}

}

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

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

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