我對 Java 很陌生,一直被困在我一直試圖創建的程式上。出于背景知識的目的,該程式適用于一家名為“Ship It”的公司,該公司是一家包裹運輸公司。用戶輸入包裹的重量,以及它將行駛的距離。公司根據重量收取每 200 英里的費用。
0 < 重量 <= 3 磅 $1.00 費用
3 < 重量 <= 6 磅 $2.00 費用
6 < 重量 <= 10 磅 $3.00 費用
10 < 重量 <= 20 磅 $4.00 費用
20 < 重量 <= 30 磅 $5.00 費用
到目前為止,這是我擁有的代碼:
public static void main(String[] args)
{
Scanner kb = new Scanner (System.in);
//Variables
double costWithoutCharge = 0, weight, distance = 0;
//Introduction to ShipIt
System.out.print("\t\t******Welcome to ShipIt******\n");
System.out.print("***We are a low-charge, secure shipping company for packages"
"up to 30 pounds***");
//User Enters Weight of Package
System.out.print("\n\nEnter the weight of the package (1.0 - 30.0 pounds): ");
weight = kb.nextDouble();
System.out.print("");
// User Enters distance the package will travel
System.out.print("Enter the miles to the destination (1 - 2000 miles): ");
distance = kb.nextInt();
System.out.print("");
//Weight If-else Statement
if (weight >30.0)
System.out.println ("\nSorry, you have entered invalid data - program terminated");
if (weight >30.0)
System.exit((int) weight);
//Distance Restriction if-else
if (distance >2000)
System.out.println ("\nSorry, you have entered invalid data - program terminated");
if (distance >2000)
System.exit((int) distance);
costWithoutCharge = distance / 200;
//If else
if (weight <0 || weight <=3)
{
System.out.println ("The cost to ship the package is: " "$" (costWithoutCharge)*1.00);
}
else if (weight <3 || weight <= 6)
{
System.out.println ("The cost to ship the package is: " "$" (costWithoutCharge)*2.00);
}
else if (weight <6 || weight <= 10)
{
System.out.println ("The cost to ship the package is: " "$" (costWithoutCharge)*3.00);
}
else if (weight <10 || weight <= 20)
{
System.out.println ("The cost to ship the package is: " "$" (costWithoutCharge)*4.00);
}
else {
System.out.println ("The cost to ship the package is: " "$" (costWithoutCharge)*5.00);
}
kb.close();
}
}
到目前為止,如果我輸入一個像 1001 這樣的值,運輸成本是 15.015 美元,但應該是 18 美元,因為費用是每 200 英里乘以的。如果我需要為每 200 英里的收費問題做一個新的等式,或者是否可以用另一個 if 陳述句來支持,我會猶豫不決?
我覺得我已經嘗試了一切,但似乎無法解決這個問題):我急需幫助!請!
uj5u.com熱心網友回復:
第一的
if (weight <0 || weight <=3)
應該
if (0 < weight && weight <=3)
但是代碼應該更容易維護,使用限制表:
double[][] weightsAndCharges = {
{3, 1.00},
{6, 2.00},
{10, 3.00},
{20, 4.00},
{30, 5.00}
};
double charge = 10.00;
for (double[] weightAndCharge : weightAndCharges) {
if (weight <= weightAndCharge[0]) {
charge = weightAndCharge[1];
break;
}
}
System.out.printf("The cost to ship the package is: $%0.2f%n", charge*distanceRatio);
uj5u.com熱心網友回復:
答案是一些基本的數學。
您正在考慮的是組合爆炸:如果您在每個重量宣告中分層一整批if/elseif宣告if,例如if (miles < 200) ... else if (miles >= 200 && miles < 400)- 然后在維度中考慮它:您有“英里”維度,目前正在添加 10 個選項(1 -200, 200-399, 400-599, etc),重量尺寸加5。
你在這里需要的 ifs 數量是 A*B: 50 ifs。
這是一噸,顯然不是你想要的。
數學來拯救!
你真的只是想計算costPerSegment * segments。
單獨計算這兩個值,現在它只是 A B: 15 ifs。鑒于您實際上可以使用數學本身將英里數轉換為您需要收費的路段數(英里部分只是除以 200,不涉及查找表),我們減少到 5 個 ifs。
另請注意,您的代碼有問題。你的體重if宣告有他們的>和<相反的。但這else if隱藏了問題。我在下面的代碼段中修復了這個問題。
double costPerSegment;
if (weight <=3) {
costPerSegment = 1.0;
} else if (weight <= 6) {
costPerSegment = 2.0;
} else if (weight <= 10) {
costPerSegment = 3.0;
} else if (weight <= 20) {
costPerSegment = 4.0;
} else {
costPerSegment = 5.0;
}
// Casting a positive double to an int automatically rounds down.
int segments = (int) miles / 200;
double cost = costPerSegment * segments;
uj5u.com熱心網友回復:
您的示例中缺少重量。聽起來在你的例子中你有:
- 距離 1001
- 重量在 6 到 10 之間,每“開始 200 英里”收費 3 美元
從您的代碼中,回傳 15.015。
看來您想計算“起始 200 英里”,因此您可以通過四舍五入來實作:
costWithoutCharge = Math.ceil( distance / 200 );
另一方面,您可能希望從 if/then/else 塊中洗掉公共部分。也就是說,只執行計算而不執行每個子句中的 System.out.println。
uj5u.com熱心網友回復:
這條線導致了輸入距離 1001 的問題
costWithoutCharge = distance / 200; // result = 5,005
據我了解,您只想在這里擁有 5 個
所以最簡單的解決方案是將 costWithoutCharge 宣告為 int 而不是
costWithoutCharge = (int) distance / 200; // result = 5
或者,如果您想將 costWithoutCharge 保持為兩倍,您可以使用 Math lib 對其進行四舍五入
costWithoutCharge = Math.round(distance / 200); // result = 5
uj5u.com熱心網友回復:
邏輯似乎沒問題。如果您的權重 = 10 且距離 = 1001,則很明顯 (1001/200) = 5.005,然后 (5.005*3) = 15.015。雖然你的代碼很笨拙,但你放錯了“>”、“<”符號,而且你不需要在里面多次提到列印陳述句,只需為所有情況放一個通用的。此外,如果您想將值四舍五入,如果您將 1001 作為距離,它在任何情況下都不會是 18。無論如何,如果你想把它四舍五入,你可以將它的型別轉換為 int。
double charge;
if (weight <=3) {
charge = 1.0;
} else if (weight >3 || weight <= 6) {
charge = 2.0;
} else if (weight >6 || weight <= 10) {
charge = 3.0;
} else if (weight >10 || weight <= 20) {
charge = 4.0;
} else {
charge = 5.0;
}
costWithoutCharge = (int) distance / 200; // Casting double to int
double total_cost = costWithoutCharge * charge;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/316341.html
標籤:爪哇 if 语句 java.util.scanner
