我不太清楚math.ceil或math.round的格式舍入到更高的整数

时间:2019-07-09 21:28:22

标签: java integer rounding

哪个数学函数最适合将用户输入四舍五入到下一个整数,并且应如何格式化?

我已经尝试过math.ceil和math.round,但是我可能会得到格式错误以及代码中的最佳位置,以便将其舍入到下一个整数。

package paintestimator;

import java.util.Scanner;
import java.lang.Math;     // Note: Needed for math functions

public class PaintEstimator {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

  double wallHeight = 0.0;
  double wallWidth = 0.0;
  double wallArea = 0.0;
  double gallonsPaintNeeded = 0.0;
  int cansNeeded = 0;

  final double squareFeetPerGallons = 350.0; 
  final double gallonsPerCan = 1.0;

  gallonsPaintNeeded = wallArea / squareFeetPerGallons;
  System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");

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

  cansNeeded = (int) (gallonsPaintNeeded / gallonsPerCan);  //Hint: this line is missing two operations

  System.out.println("Cans needed: " + cansNeeded + " can(s)");  


   }
}

输出不同。

Input
12
15

Your output
Enter wall height (feet): 
Enter wall width (feet): 
Wall area: 180.0 square feet
Paint needed: 0.5142857142857142 gallons
Cans needed: 0 can(s)

Expected output
Enter wall height (feet): 
Enter wall width (feet): 
Wall area: 180.0 square feet
Paint needed: 0.5142857142857142 gallons
Cans needed: 1 can(s)

0 个答案:

没有答案