解题思路:输入的数加上0.5,然后强制转化为整数,利用int强转会舍弃小数点后的特性可实现四舍五入功能。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextDouble()) { // 注意 while 处理多个 case
double a = in.nextDouble();
int b =(int)(a+0.5);
System.out.println(b);
}
}
}
直接调用Math.round()方法实现四舍五入功能:
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextDouble()) { // 注意 while 处理多个 case
double a = in.nextDouble();
System.out.println(Math.round(a));
}
}
}



京公网安备 11010502036488号