将double转换为int或甚至小数位

时间:2015-10-25 00:46:03

标签: java

我想知道是否有人可以告诉我 1.为什么,当我输入weightNumber的小数位时,weightConverted不会将它转换为整数,即使我为它创建变量? 2.我怎么能以任何方式改进这个“程序”,谢谢!!

问题在于:

enter image description here

import java.util.Scanner;
public class cofee {

    public static void main (String []args){


        double weightNumber = 0;
        String packageType = "";
        String serviceType ="";
        double totalFee = 0;
        double weightConverted = Math.round(weightNumber); // <- this is the problem, should i put it somewhere else?

        final double LETTERCOSTP = 12.00;
        final double LETTERCOSTS = 10.50;
        final double BOXCOSTP = 15.75;
        final double BOXCOSTS = 13.75;
        final double BOXWEIGHTP = 1.25;
        final double BOXWEIGHTS = 1.00;

        // input
        Scanner input = new Scanner(System.in);

        System.out.print("Enter package type (letter/box): ");
            packageType = input.nextLine().toLowerCase();

        System.out.print("Enter type of service (standard/priority): ");
            serviceType = input.nextLine().toLowerCase();

        switch(packageType)
        {
        case "letter":
            System.out.print("Enter the weight in ounces: ");
            weightNumber = input.nextDouble();
            break;

        case "box":
            System.out.print("Enter the weight in pounds: ");
            weightNumber = input.nextDouble();
            break;

            default:
                System.out.print("WRONG PACKAGE TYPE !!!");
        }



            // letter
        if (packageType.equals("letter") && serviceType.equals("priority"))
        {
            totalFee = LETTERCOSTP;
        }
        if (packageType.equals("letter") && serviceType.equals("standard"))
        {
            totalFee = LETTERCOSTS;
        }


            // box
        if (packageType.equals("box") && serviceType.equals("priority"))
        {
            totalFee = BOXCOSTP + ((weightConverted - 1.0) * BOXWEIGHTP);
        }
        if (packageType.equals("box") && serviceType.equals("standard"))
        {
            totalFee = BOXCOSTS + ((weightConverted - 1.0) * BOXWEIGHTS);
        }





        // display

        System.out.println("The fee is € "+ totalFee + " for a package with");
        System.out.println("\tType: "+packageType);
        System.out.println("\tService: "+serviceType);
        System.out.println("\tOunces: "+weightConverted);




    }

}

1 个答案:

答案 0 :(得分:0)

Exec dbo.usr_GetLastDate @TableName, @LastTransDate OUTPUT EXEC dbo.usr_GetRecCount @TableName, @RecCount OUTPUT 行会调用double weightConverted = Math.round(weightNumber);,其值为round()weightNumber ,因此会围绕0到... ... ... 0,并将其分配给0