zoukankan      html  css  js  c++  java
  • java蓝桥杯--入门训练(2)圆的面积

    关键字:入门 实数输出

    题目:

    解决方法:

    方法一(我的):

     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 }
  • 相关阅读:
    Linux 中文件名颜色所代表的属性
    time manage
    NoClassDefFoundError
    swagger在线文档
    2020.8.18
    spring jpa data的关键字
    2020.8.6
    spring data jpa的报错Can not set int field XXX to null value
    deadlock found when trying to get lock ;try restarting transaction
    查找-斐波那契
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/8481974.html
Copyright © 2011-2022 走看看