我需要帮助理解名为Shipping Charges的编程挑战

时间:2016-09-30 17:11:05

标签: java magento

问题是快速货运公司收取以下费率:

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

1 个答案:

答案 0 :(得分:0)

这将为您服务

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));
            }

        }