magento 我需要帮助了解称为Shipping Charges的编程难题

ntjbwcob  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(159)

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

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
sh7euo9m

sh7euo9m1#

这将为您工作

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

        }
8dtrkrch

8dtrkrch2#

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
    // write your code here
        int weight = 0;
        double distance = 0.0 ;
        Scanner keyboard =new Scanner(System.in);

        System.out.println("Enter the Distance");
        distance = keyboard.nextDouble();

        System.out.println("Enter the Weight");
        weight = keyboard.nextInt();

        if (weight <= 2) {
            System.out.println("charge is : " + "$"+1.10);
        }
        else if (weight > 2 && weight <= 6)
        {
            System.out.println("charge is : " + "$"+2.20);
        }
        else if (weight > 6 && weight <= 10)
        {
            System.out.println("charge is : " + "$"+3.70);

        }
        else if (weight > 10)
        {
            System.out.println("charge is :" + "$"+4.80);
        }

    }
    }

相关问题