[英]While-Loop completion issue and final value calculations
我在使用循環語句時遇到問題,如果沒有恢復原來的循環條件,就無法正常執行。 此外,如果我希望我的最終輸出包含所購買產品總數的定價值,我將如何實現這一目標?
最終產品需求:客戶購買所有9件商品
請輸入你的名字:John Smith
GRAPEFRUIT產品
- gPod shuffle $ 49
- gPod Touch $ 299
- gPad Mini $ 329
- gPad 2 399美元
- gPhone 199美元
- gMac 1299美元
- MacNovel Pro 1199美元
- MacNovel Air $ 999
- MiniMac 599美元
- 完成我的訂單
請從上面的菜單中選擇一個項目:5
請從上面的菜單中選擇另一個項目:2
請從上面的菜單中選擇另一個項目:7
請從上面的菜單中選擇另一個項目:9
請從上面的菜單中選擇其他項目:3
請從上面的菜單中選擇其他項目:4
請從上面的菜單中選擇其他項目:6
請從上面的菜單中選擇其他項目:1
請從上面的菜單中選擇另一個項目:8
請從上面的菜單中選擇另一個項目:10感謝您訂購Grapefruit Company,John Smith
訂購的商品總數:9
訂購的商品價格:5371美元
銷售稅:349.115美元
應付總額:5720.115美元
這是我的代碼:
public static void main(String[] args) {
// Declare Variables
Scanner input = new Scanner (System.in);
String CustomerName;
int gpodShuffle = 1;
int gpodTouch = 2;
int gpadMini = 3;
int gpadTwo = 4;
int gphone = 5;
int gmac = 6;
int macnovelPro = 7;
int macnovelAir = 8;
int miniMac = 9;
int nNumber = 0;
int nProducts = 0;
int nTotal = 0;
//Declare Constants
final int SENTINEL = 10;
final double SALES_TAX = 6.5;
final int GPOD_SHUFFLE = 49;
final int GPOD_TOUCH = 299;
final int GPAD_MINI = 329;
final int GPAD_TWO = 399;
final int GPHONE = 199;
final int GMAC = 1299;
final int MAC_NOVELPRO = 1199;
final int MAC_NOVELAIR = 999;
final int MINI_MAC = 599;
//Prompt user to enter name
System.out.println("Please enter your name: ");
//Enter user name
CustomerName = input.nextLine();
//Print Blank Line
System.out.println("");
//Begin Product Listing
System.out.println("GRAPEFRUIT PRODUCT:");
System.out.println("1. gPod shuffle $49");
System.out.println("2. gPod Touch $299");
System.out.println("3. gPad Mini $329");
System.out.println("4. gPad 2 $399");
System.out.println("5. gPhone $199");
System.out.println("6. gMac $1299");
System.out.println("7. MacNovel Pro $1199");
System.out.println("8. MacNovel Air $999");
System.out.println("9. MiniMac $599");
System.out.println("10. Complete my order");
//Keep reading until the input is 10
while (nNumber != SENTINEL) {
//Calculate entered items
nTotal = nTotal + nNumber;
nProducts++;
System.out.println("\nPlease select an item from the menu above: ");
//Read number entered by the user
nNumber = input.nextInt();
if (nNumber == SENTINEL)
System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName);
else if (nNumber != SENTINEL)
System.out.println("Please select another item from the menu above: ");
} //End Loop
//Process selections entered by the user
//Increment count
//Print blank line to screen
System.out.println("");
//Total amount of product ordered
System.out.println("Total items ordered: ");
//Total price of items ordered
System.out.println("Price of items ordered: ");
//Sales tax associated with the purchase
System.out.println("Sales tax: " + SALES_TAX);
//Total amount due by the customer to Grapefruit Co.
System.out.println("Total amount due: ");
}
}
對於某些要添加的類來說,這非常令人煩惱,因此您可以輕松地遍歷這些項目。
不過你可以在這個場景中做些什么:
保持數組中項目的價格。
int [] itemPrices = {49,299,329,399,199,1299,1199,999,599};
更新您的打印報表
System.out.println("1. gPod shuffle $" + prices[0]); System.out.println("2. gPod Touch $" + prices[1]); System.out.println("3. gPad Mini $" + prices[2]); System.out.println("4. gPad 2 $" + prices[3]); System.out.println("5. gPhone $" + prices[4]); System.out.println("6. gMac $" + prices[5]); System.out.println("7. MacNovel Pro $" + prices[6]); System.out.println("8. MacNovel Air $" + prices[7]); System.out.println("9. MiniMac $" + prices[8]);
更新while循環以引用這些項目價格
// Keep reading until the input is 10 while (nNumber != SENTINEL) { System.out.println("\\nPlease select an item from the menu above: "); // Read number entered by the user nNumber = input.nextInt(); if (nNumber == SENTINEL) { System.out.println("Thank you for ordering with Grapefruit Company, " + CustomerName); // The user's just entered the value held for SENTINEL - leave the while loop. break; } // Calculate the total price. nTotal = nTotal + prices[nNumber-1]; // Increment the total number of products entered. nProducts++; }
更新您的最終打印聲明:
//Total amount of product ordered System.out.println("Total items ordered: " + nProducts); //Total price of items ordered System.out.println("Price of items ordered: "+nTotal); //Sales tax associated with the purchase System.out.println("Sales tax: " + SALES_TAX);
我會告訴你如何獲得總金額:)
要解決此連接問題,您遇到的問題是:
System.out.println("Total amount due: " + ((SALES_TAX * nTotal) + nTotal));
這意味着在將其轉換為字符串之前執行完整計算。
但背后的數學是錯誤的,而不是做類似如下:
// Calculate the amount of tax.
double salesTax = (SALES_TAX/100) * nTotal;
System.out.println("Sales tax: " + salesTax);
你應該這樣做:
//Total amount due by the customer to Grapefruit Co.
System.out.println("Total amount due: " + (salesTax + nTotal));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.