【发布时间】: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