关键字:入门 实数输出
题目:
解决方法:
方法一(我的):
1 import java.text.DecimalFormat; 2 import java.util.Scanner; 3 4 import static java.lang.StrictMath.atan; 5 6 public class Main{ 7 public static void main(String[] args){ 8 double PI=atan(1.0)*4; 9 Scanner read =new Scanner(System.in ); 10 int r= read.nextInt(); 11 double area= PI*r*r; 12 //Java中规定精确度 13 DecimalFormat df = new DecimalFormat("#0.0000000"); 14 System.out.println(df.format(area)); 15 } 16 }
方法二:(小丽丽)
1 import java.util.Scanner; 2 import java.math.BigDecimal; 3 public class Main { 4 5 public static void main(String[] args){ 6 Scanner scanner = new Scanner(System.in); 7 int radius = scanner.nextInt(); 8 String result = circleArea(radius); 9 System.out.println(result); 10 } 11 12 //https://zhidao.baidu.com/question/402552052.html 保留7位. 13 // area = Math.PI * radius * radius. 14 private static String circleArea(double radius){ 15 double result = Math.PI * Math.pow(radius, 2); 16 17 BigDecimal bigDecimal = new BigDecimal(result); 18 result = bigDecimal.setScale(7, BigDecimal.ROUND_HALF_UP).doubleValue(); 19 20 return String.format("%.7f", result); 21 22 } 23 }