题解 | #取近似值#
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
- 接收 double类型的数据输入:Double num = sc.nextDouble();
- Double可以强转成 (int)
- 转成int类型直接会舍弃小数点,只保留整数位。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Double num = sc.nextDouble(); int number = (int) (num + 0.5); System.out.println(number); } }