我有这个代码,我正在做,不能解释为什么它没有正确循环并将产品的数量加在一起似乎它只读取while循环中的第一个语句这就是它所带来的所有数据。我还需要它不要继续循环回第一个while语句,而是继续使用相同的短语"请从上面的菜单中输入另一个项目:"任何帮助表示赞赏,谢谢你们。
import java.util.Scanner;
public static void main(String[] args) { //Declare Variables
Scanner input = new Scanner(System.in);
int nProduct = 0; //Stores the value entered by the user
int nPrice = 0; //Stores sum of values entered
int nCount = 0;
int nSum = 0;
double dTax = 0.0;
double dTotal = 0.0;
final int SENTINEL = 10; //Used to end loop
final double TAX = .065;
System.out.print("Please enter the your name: ");
String sName = input.nextLine( );
System.out.println("");
System.out.println("BEST PURCHASE PRODUCTS: ");
System.out.println("1. Smartphone $249");
System.out.println("2. Smartphone Case $39");
System.out.println("3. PC Laptop $1149");
System.out.println("4. Tablet $349");
System.out.println("5. Tablet Case $49");
System.out.println("6. eReader $119");
System.out.println("7. PC Desktop $889");
System.out.println("8. LED Monitor $299");
System.out.println("9. Laser Printer $399");
System.out.println("10.Complete my order");
while (nProduct != SENTINEL) {
nSum = nPrice + nSum;
nCount++;
System.out.print("Please enter item from the menu above: ");
nProduct = input.nextInt();
if (nProduct == 1) {
nPrice += 249;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 2) {
nPrice += 39;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 3 ) {
nPrice += 1149;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 4 ) {
nPrice += 349;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 5 ) {
nPrice += 49;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 6 ) {
nPrice += 119;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 7 ) {
nPrice += 899;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if (nProduct == 8 ) {
nPrice += 299;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
else if(nProduct == 9 ) {
nPrice += 399;
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
}
}
dTax = (nPrice * TAX);
dTotal = dTax + nPrice;
System.out.println("");
System.out.println("Thank you for ordering with Best Purchase,"+sName);
System.out.println("Total Items Ordered: " + nCount);
System.out.println("Price of items ordered: $" + nSum);
System.out.println("Sales Tax: $" + dTax);
System.out.println("Total amount due: $" + dTotal);
}
答案 0 :(得分:1)
每个input.nextInt()
块中的附加if
在这里没有任何意义。这样,您必须在每次迭代中输入两个数字。以下循环应该执行您想要的操作:
System.out.print("Please enter item from the menu above: ");
while ((nProduct = input.nextInt()) != SENTINEL) {
nSum = nPrice + nSum;
nCount++;
System.out.print("Please enter another item from the menu above: ");
if (nProduct == 1) {
nPrice += 249;
}
else if (nProduct == 2) {
nPrice += 39;
}
else if (nProduct == 3 ) {
nPrice += 1149;
}
else if (nProduct == 4 ) {
nPrice += 349;
}
else if (nProduct == 5 ) {
nPrice += 49;
}
else if (nProduct == 6 ) {
nPrice += 119;
}
else if (nProduct == 7 ) {
nPrice += 899;
}
else if (nProduct == 8 ) {
nPrice += 299;
}
else if(nProduct == 9 ) {
nPrice += 399;
}
}
我不确定您要使用语句nSum = nPrice + nSum;
完成什么。这计算所有部分和的总和。
答案 1 :(得分:0)
您可以移动提示&在整个if / else业务之后读取输入,以便(a)你只需要写一次,(b)即使nProduct
不匹配,它也会完成产品。但是你需要移动第一个提示&在循环开始之前读取。
答案 2 :(得分:0)
我同意@ scott-hunter的解决方案,但是另一种方法可以改变提示,以便你有这样的东西:
String prompt = "Please enter item from the menu above: ";
while (nProduct != SENTINEL) {
// Some code ...
System.out.println(prompt);
// ... more code
prompt = "Please enter another item from the menu above: ";
}
答案 3 :(得分:0)
你应该拿出
System.out.print("Please enter another item from the menu above: ");
nProduct = input.nextInt();
来自每个if语句,这应该可以解决您的问题。
基本上,你这样做:
如果从每个if语句中删除上述代码,您将执行
你想要的是什么。
答案 4 :(得分:0)
我同意弗兰克......但看起来nSum不会在这里正确出现。
而不是......
nPrice += xxx;
在每个声明中......你应该简单地把
nSum += xxx;
并停止在顶部再次评估总和。正如您现在所拥有的那样,您不断增加nPrice的价值。另一种选择是在每个if语句中简单地设置nPrice,并在循环的顶部或底部评估nSum。
nPrice = xxx;
您还获得了额外的计数,因为您在实际阅读该项目之前正在处理nCount ++。这意味着如果您输入“10”退出,则表示您已添加了一个项目。
你可以尝试这样的事情。它很乱,花了我5分钟,可能不是最好的方法,但它应该有效:
System.out.print("Please enter item from the menu above: ");
nProduct = input.nextInt()
while (nProduct != SENTINEL) {
nProduct = input.nextInt()
if (nProduct == 1) {
nSum += 249;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 2) {
nSum += 39;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 3 ) {
nSum += 1149;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 4 ) {
nSum += 349;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 5 ) {
nSum += 49;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 6 ) {
nSum += 119;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 7 ) {
nSum += 899;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if (nProduct == 8 ) {
nSum += 299;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
else if(nProduct == 9 ) {
nSum += 399;
System.out.print("Please enter another item from the menu above: ");
nCount++;
}
}