java - 我需要帮助理解称为运费的编程挑战

标签 java magento

问题表明 Fast Freight Shipping Company 收取以下费率:

Weight of Package                                       Rate per 500 Miles Shipped
2 pounds or less                                        $1.10
Over 2 pounds but not more than 6 pounds                $2.20
Over 6 pounds but not more than 10 pounds               $3.70
Over 10 pounds                                          $3.80

每 500 英里的运费不按比例计算。例如,如果 2 磅重的包裹运送 550 英里,则费用为 2.20 美元。编写一个程序,要求用户输入包裹的重量,然后显示运费。

我的问题是,每次输入重量和距离时,我都会收到两个不同的答案。例如,当我输入重量为 2 磅、距离为 500 英里时,我得到的答案为 0.0 美元和 3.8 美元,这两个答案都是错误的。看起来我输入的一些权重是正确的答案,而我输入的其他权重却给了我错误的答案。这是我的程序:

//import java utilities for scanner class
import java.util.Scanner;           



public class ShippingCharge

{

public static void main (String[] args)

{


//Declare and initialize variable to hold the entered weight.
int weight = 0;                                        

//Declare and initialize variable to hold the entered distance.
double distance = 0.0;                                

//This variable will hold the calculated rate.
double rate; 

//This will decide if the shipping charge will advance up one level.
int distanceMultiplier = (int)distance / 500;  

//This will hold the increments of the shipping charge.
int distanceRemainder;                                  

//Create a Scanner object for the input.
Scanner input = new Scanner(System.in);        

//Get the weight of the package.
System.out.println("What is the weight of the package (in pounds)?");

weight = input.nextInt();

//Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles)?");

distance = input.nextDouble();

distanceRemainder = (int)distance % 500;
if (distanceRemainder == 0)
{
if (weight <= 2)
    System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 1.10));
    }
else if (weight > 2 && weight <= 6)
{
    System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 2.20));
    }
else if (weight > 6 && weight <= 10)
{
    System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.70));
    }
else
{
    System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.80));
    }

if (distanceRemainder != 0)
{
    if (weight <= 2)
    System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 1.10);
    }
else if (weight > 2 && weight <= 6)
{
    System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 2.20);
    }
else if (weight > 6 && weight <= 10)
{

    System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.70);

    }
else
{
    System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.80);

    }

 //end program
 System.exit(0);

 }//end main
 }//end class

最佳答案

这对你有用

public static void main(String[] args) {

            int weight = 0;
            double distance = 0.0 , distanceExtra ;

            Scanner in = new Scanner(System.in);
            System.out.println("Weight ? ");
            weight = in.nextInt();

            System.out.println("Distance ? ");
            distance = in.nextDouble();
            distanceExtra = distance / 500;
            distanceExtra = Math.ceil(distanceExtra);

            if (weight <= 2) {
                    System.out.printf("charge is :" , (distanceExtra * 1.10));
            } 
            else if (weight > 2 && weight <= 6)
            {
                    System.out.printf("charge is :" , (distanceExtra * 2.20));
            } 
            else if (weight > 6 && weight <= 10) 
            {
                    System.out.printf("charge is :" , (distanceExtra * 3.70));

            } 
            else if (weight > 10) 
            {
                    System.out.printf("charge is :" , (distanceExtra * 4.80));
            }

        }

关于java - 我需要帮助理解称为运费的编程挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796672/

相关文章:

java - JMX MBean - 在 getter 和 setter 上同步

java - 在 ANTLR v4 中定义 double 类型的规则

database - Magento 通过 CSV 导入产品图片媒体

php - 如何在 Magento 列表产品页面上获取属性值的 ID 号?

Magento:从 magento 外部登录并重定向到帐户页面

java - Android:在 API 级别 9 中使用 PopupMenu

java - Maven 程序集插件 : Resources are copied at root of jar

java - CardLayout 管理器问题/困惑

magento - Magento与第三方CRM,POS和ERP解决方案的集成

Magento 添加步骤到单页结帐