尝试在 java 中编写一个简单的程序来使用 else if 语句计算运费,但我完全迷路了
Trying to write a simple program in java to calculate shipping price using else if statements and i'm thoroughly lost
对于我在 java class 中的作业,我们应该编写一个运费计算器来根据每 "weight class" 每“500 英里 "flat rates" 计算最终运费已发货”的说明是这样的。
The Fast Freight Shipping Computer charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less .10
Over 2 pounds but not more than 6 pounds .20
Over 6 pounds but not more than 10 pounds .70
Over 10 pounds .80
The shipping charges per 500 miles are not prorated. For example, if a 2 pound
package is shipped 550 miles, the charges would be .20. Write a program that asks
the user to enter the weight of a package and the distance traveled,
and then displays the shipping charges.
我的老师希望我们利用 "else" 和 "if" 来完成这项作业,但我他不会介意它是否更高级一点,他实际上是建议使用堆栈溢出来寻求帮助的人,所以在这里我上午
基本上我不知道如何让它每 500 英里收费并将其应用到程序中。如果有人可以提供帮助,我将不胜感激,并想看看您是如何想出来的,以及为什么要按照您的方式去做。
很抱歉这个冗长的问题,但我不知所措。我已经尝试了很多次并且从头开始重新启动的次数比我想承认的要多这是我最近的尝试:
public static void main(String[] args) {
// Declarations
double finalPrice;
double shippingRate;
int milesShipped;
double packageWeight;
Scanner keyboard = new Scanner(System.in);
// Get user input
System.out.print("Please enter the weight of package in Pounds: ");
packageWeight = keyboard.nextDouble();
System.out.print("Please enter the miles traveled in miles: ");
milesShipped = keyboard.nextInt();
//Computations
if (packageWeight <= 2) {
shippingRate = 1.10;
}else if (packageWeight >= 2 && packageWeight <= 6) {
shippingRate = 2.20;
}else if (packageWeight >= 6 && packageWeight <= 10) {
shippingRate = 3.70;
}else {
shippingRate = 3.80;
}
if (milesShipped <= 500) {
finalPrice = shippingRate;
}else if (milesShipped >= 500.0 && milesShipped <= 1000.0) {
finalPrice = shippingRate;
}
///Display results
System.out.println("your shipping charges are calculated to be " + '$'+ finalPrice );
移除
if (milesShipped <= 500) {
finalPrice = shippingRate;//this never updates for each 500 miles
}else if (milesShipped >= 500.0 && milesShipped <= 1000.0) {
finalPrice = shippingRate;
}
用下面的代码替换它
while (milesShipped > 0) {
finalPrice += shippingRate;
milesShipped -= 500;//subtract 500 from the miles till it is less than 0
}
代码将 运行 直到它小于 0,在此之前循环的每次迭代都会将运费添加到每 500 件的最终价格中。
假设用户为 2 磅的包裹输入 1500
first iteration
finalPrice = 1.10
milesShipped-500 = 1000
second iteration
finalPrice = 2.20
milesShipped-500 = 500
third iteration
finalPrice = 3.20
milesShipped-500 = 0
the loop now terminates since milesShipped is no longer greater than 0.
对于我在 java class 中的作业,我们应该编写一个运费计算器来根据每 "weight class" 每“500 英里 "flat rates" 计算最终运费已发货”的说明是这样的。
The Fast Freight Shipping Computer charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less .10
Over 2 pounds but not more than 6 pounds .20
Over 6 pounds but not more than 10 pounds .70
Over 10 pounds .80
The shipping charges per 500 miles are not prorated. For example, if a 2 pound
package is shipped 550 miles, the charges would be .20. Write a program that asks
the user to enter the weight of a package and the distance traveled,
and then displays the shipping charges.
我的老师希望我们利用 "else" 和 "if" 来完成这项作业,但我他不会介意它是否更高级一点,他实际上是建议使用堆栈溢出来寻求帮助的人,所以在这里我上午
基本上我不知道如何让它每 500 英里收费并将其应用到程序中。如果有人可以提供帮助,我将不胜感激,并想看看您是如何想出来的,以及为什么要按照您的方式去做。
很抱歉这个冗长的问题,但我不知所措。我已经尝试了很多次并且从头开始重新启动的次数比我想承认的要多这是我最近的尝试:
public static void main(String[] args) {
// Declarations
double finalPrice;
double shippingRate;
int milesShipped;
double packageWeight;
Scanner keyboard = new Scanner(System.in);
// Get user input
System.out.print("Please enter the weight of package in Pounds: ");
packageWeight = keyboard.nextDouble();
System.out.print("Please enter the miles traveled in miles: ");
milesShipped = keyboard.nextInt();
//Computations
if (packageWeight <= 2) {
shippingRate = 1.10;
}else if (packageWeight >= 2 && packageWeight <= 6) {
shippingRate = 2.20;
}else if (packageWeight >= 6 && packageWeight <= 10) {
shippingRate = 3.70;
}else {
shippingRate = 3.80;
}
if (milesShipped <= 500) {
finalPrice = shippingRate;
}else if (milesShipped >= 500.0 && milesShipped <= 1000.0) {
finalPrice = shippingRate;
}
///Display results
System.out.println("your shipping charges are calculated to be " + '$'+ finalPrice );
移除
if (milesShipped <= 500) {
finalPrice = shippingRate;//this never updates for each 500 miles
}else if (milesShipped >= 500.0 && milesShipped <= 1000.0) {
finalPrice = shippingRate;
}
用下面的代码替换它
while (milesShipped > 0) {
finalPrice += shippingRate;
milesShipped -= 500;//subtract 500 from the miles till it is less than 0
}
代码将 运行 直到它小于 0,在此之前循环的每次迭代都会将运费添加到每 500 件的最终价格中。
假设用户为 2 磅的包裹输入 1500
first iteration
finalPrice = 1.10
milesShipped-500 = 1000
second iteration
finalPrice = 2.20
milesShipped-500 = 500
third iteration
finalPrice = 3.20
milesShipped-500 = 0
the loop now terminates since milesShipped is no longer greater than 0.