我有一个问题。我基本上完成了我的程序,除了我创建的while循环的最后一个中断。如果我输入-1以突破我的第一个输出问题“输入产品编号(1-5)或-1退出”,它会突破而没有问题。如果我在第二个问题“输入数量或-1退出”期间输入-1,它只会循环回到下一个产品编号请求。
我已经尝试查找有关打破while循环的一些其他信息,并尝试将相同的逻辑应用于我现有的代码。不幸的是,我仍然遇到同样的问题。它没有出现这个特殊的请求是一个重复的问题,所以我觉得安全问它。我做错了什么,因为经过大约一个小时的努力解决这个问题后,我只是疯了。任何方向将不胜感激。
谢谢,
PROG
import java.util.Scanner;
public class 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
}
}
}
答案 0 :(得分:0)
在switch语句中不需要case -1。相反,你可以添加pQuantity的条件&gt; 1进入while条件如下:
while ((pNumber >= 0 && pNumber <=5) || ( pQuantity >1))
现在,如果你输入-1到第一个问题,它退出程序不是因为最后一个案例-1条件,而是因为while循环条件。但是当您在第二个问题(产品数量)中输入-1时,它将转到相应的大小写条件,完成并退出switch语句并返回while循环。它不会去案例-1条件检查。