题目叙述:给定圆的半径r,求圆的面积。
详细要求:
解题思路:
- 重点是结果严格输出7位小数,这里可以参考该博客: https://blog.csdn.net/iteye_3260/article/details/82602343.
- 在使用System.out.println()或System.out.print()输出字符常量时,不可以出现“回车”,所以运用System.out.printf(“格式控制部分”,表达式1,表达式2···)格式输出。
代码详情:
方法一: 直接定义PI进行计算
import java.util.Scanner;
class Circular{
public static void main(String[] args){
final double PI=3.14159265358979323;
Scanner sc=new Scanner(System.in);
int r=sc.nextInt(); //输入整数r表示半径
double s=PI*r*r;
System.out.printf("%.7f",s);
}
}
方法一:利用 java.lang包中的Math类中的static常量PI
import java.util.Scanner;
class Circular{
public static void main(String[] args){
//final double PI=3.14159265358979323;
Scanner sc=new Scanner(System.in);
int r=sc.nextInt();
double s=Math.PI*r*r;
System.out.printf("%.7f",s);
}
}