求正多边形的面积
代码如下:
import java.util.Scanner; class Home21_1{ public static void main(String[]args){ Scanner scanner=new Scanner(System.in); System.out.print('Enter the number of sides:'); int n=scanner.nextInt(); System.out.print('Enter the side:'); double side=scanner.nextDouble(); double area=area(n,side); System.out.println('The area of the polygon is '+area); } public static double area(int n,double side){ double area=n*side*side/(4*Math.tan(Math.PI/n)); return area; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
代码解析:题目中已经给出了求面积公式,只需要提示用户输入未知量n和side即可代入公式求解,该题用到了函数重载思想。
赞 (0)