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

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

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

问题

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

  1. public class PaintEstimator {
  2. public static void main(String[] args) {
  3. // 创建 Scanner 对象 scnr
  4. Scanner scnr = new Scanner(System.in);
  5. // 声明标识符
  6. double wallHeight;
  7. double wallWidth;
  8. double wallAera;
  9. double gallonPaint = 350;
  10. int paintNeeded;
  11. // 提示用户输入墙的高度和宽度,并显示它们
  12. System.out.println("输入墙的高度(英尺):");
  13. wallHeight = scnr.nextDouble();
  14. System.out.println("输入墙的宽度(英尺):");
  15. wallWidth = scnr.nextDouble();
  16. System.out.println("墙的高度为:" + wallHeight + ",墙的宽度为:" + wallWidth);
  17. // 计算并输出墙的面积
  18. wallAera = wallHeight * wallWidth;
  19. System.out.println("墙的面积:" + wallAera + " 平方英尺");
  20. // 计算并输出涂漆墙面所需的油漆量(以加仑为单位)
  21. System.out.println("所需油漆量:" + (wallAera / gallonPaint) + " 加仑");
  22. // 计算并输出用于涂漆的 1 加仑油漆桶的数量,四舍五入至最接近的整数
  23. System.out.println("所需油漆桶数量:" + Math.ceil(wallAera / gallonPaint) + " 桶");
  24. // 计算并输出涂漆墙壁的成本。
  25. double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
  26. System.out.println("涂漆墙壁的成本:$" + costPaint);
  27. }
  28. }
英文:

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.

  1. public class PaintEstimator {
  2. public static void main(String[] args) {
  3. // Create the Scanner object scnr
  4. Scanner scnr = new Scanner(System.in);
  5. // Declare the identifiers
  6. double wallHeight;
  7. double wallWidth;
  8. double wallAera;
  9. double gallonPaint = 350;
  10. int paintNeeded;
  11. // Prompt user for and input wall's height and width; and then display them
  12. System.out.println("Enter wall height (feet) :");
  13. wallHeight = scnr.nextInt();
  14. System.out.println("Enter wall width (feet) :");
  15. wallWidth = scnr.nextInt();
  16. System.out.println("Wall height is: " + wallHeight + " and Wall width is: " + wallWidth);
  17. // Calculate and output wall area
  18. wallAera = wallHeight * wallWidth;
  19. System.out.println("Wall area: " + wallAera + "square feet");
  20. // Calculate and output the amount of paint in gallons needed to paint the wall
  21. System.out.println("Paint needed: " + (wallAera / gallonPaint) + " gallons");
  22. // Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
  23. System.out.println("Cans needed: " + Math.ceil(wallAera / gallonPaint) + " can ( s )");
  24. // Calculate and output the cost of painting the wall.
  25. double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
  26. System.out.println("Cost to paint the wall: $" + costPaint);
  27. }
  28. }

答案1

得分: 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:

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

答案2

得分: 0

以下是翻译好的内容:

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

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

方法1:类型转换

示例:

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

方法2:四舍五入

示例:

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

方法3:Double.intValue

示例:

  1. 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:

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

Method 2: Rounding

Example:

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

Method 3: Double.intValue

Example:

  1. 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:

确定