如何更改数字的类型?从 Double 变为 int。

huangapple go评论85阅读模式
英文:

How do I change the type of number? Double to int

问题

以下是您要求的代码部分翻译:

public class PaintEstimator {
    public static void main(String[] args) {

        // 创建 Scanner 对象 scnr
        Scanner scnr = new Scanner(System.in);

        // 声明标识符
        double wallHeight;
        double wallWidth;
        double wallAera;
        double gallonPaint = 350;
        int paintNeeded;

        // 提示用户输入墙的高度和宽度,并显示它们
        System.out.println("输入墙的高度(英尺):");
        wallHeight = scnr.nextDouble();
        System.out.println("输入墙的宽度(英尺):");
        wallWidth = scnr.nextDouble();
        System.out.println("墙的高度为:" + wallHeight + ",墙的宽度为:" + wallWidth);

        // 计算并输出墙的面积
        wallAera = wallHeight * wallWidth;
        System.out.println("墙的面积:" + wallAera + " 平方英尺");

        // 计算并输出涂漆墙面所需的油漆量(以加仑为单位)
        System.out.println("所需油漆量:" + (wallAera / gallonPaint) + " 加仑");

        // 计算并输出用于涂漆的 1 加仑油漆桶的数量,四舍五入至最接近的整数

        System.out.println("所需油漆桶数量:" + Math.ceil(wallAera / gallonPaint) + " 桶");
    
        // 计算并输出涂漆墙壁的成本。
        double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
        System.out.println("涂漆墙壁的成本:$" + costPaint);
    }
}
英文:

When I run this program, I need the part where it says example " cans needed 3.0" I need it to only say the integer.

public class PaintEstimator {
    public static void main(String[] args) {

        // Create the Scanner object scnr
        Scanner scnr = new Scanner(System.in);

        // Declare the identifiers
        double wallHeight;
        double wallWidth;
        double wallAera;
        double gallonPaint = 350;
        int paintNeeded;

        // Prompt user for and input wall's height and width; and then display them
        System.out.println("Enter wall height (feet) :");
        wallHeight = scnr.nextInt();
        System.out.println("Enter wall width (feet) :");
        wallWidth = scnr.nextInt();
        System.out.println("Wall height is: " + wallHeight + " and Wall width is: " + wallWidth);

        // Calculate and output wall area
        wallAera = wallHeight * wallWidth;
        System.out.println("Wall area: " + wallAera + "square feet");

        // Calculate and output the amount of paint in gallons needed to paint the wall
        System.out.println("Paint needed: " + (wallAera / gallonPaint) + " gallons");


        // Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer


        System.out.println("Cans needed: " + Math.ceil(wallAera / gallonPaint) + " can ( s )");

        // Calculate and output the cost of painting the wall.
        double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
        System.out.println("Cost to paint the wall: $" + costPaint);
    }
}

答案1

得分: 1

你可以像你已经在做的那样四舍五入/向上取整,然后将其转换为整数:

System.out.println("需要的油漆桶数:" + (int) Math.ceil(wallAera / gallonPaint) + " 桶");
英文:

You can round/ceil it up, as you are already doing, and then cast it to an int:

System.out.println("Cans needed: " + (int) Math.ceil(wallAera / gallonPaint) + " can ( s )");

答案2

得分: 0

以下是翻译好的内容:

有三种基本方法可以将双精度数转换为整数。

其中 var-name 是整数,var-name1 是双精度数。

方法1:类型转换

示例:

int var-name = (int) var-name1;

方法2:四舍五入

示例:

int var-name = Math.round(var-name1);

方法3:Double.intValue

示例:

int var-name = Double.intValue(var-name1);
英文:

There are 3 basic methods for change a double to an integer.

Where var-name is an integer and var-name1 is a double.

Method 1: Type-casting

Example:

int var-name = (int) var-name1;

Method 2: Rounding

Example:

int var-name = Math.round(var-name1);

Method 3: Double.intValue

Example:

int var-name = Double.intValue(var-name1);

huangapple
  • 本文由 发表于 2020年9月14日 03:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63874754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定