【Java自學】計算正多邊形的面積


 1 package codeTask_FangFa;
 2 //5.45      計算五邊形的面積,正五邊形。提示用戶輸入邊的長度。
 3 import java.util.Scanner;
 4 public class AreaOfRectangle {
 5     public static void main(String[] args){
 6         Scanner input = new Scanner(System.in);
 7         System.out.println("請輸入多邊形的邊數及其長度,程序將為你顯示它的面積:");
 8         int n = input.nextInt();
 9         if(n<3){
10             System.out.println("錯誤的邊輸入!");
11             System.exit(0);
12         }
13         int side = input.nextInt();
14         if(side<0){
15             System.out.println("錯誤的邊長輸入!");
16             System.exit(0);
17         }
18         double area  = area(n,side);
19         System.out.printf("此%3d邊形的面積是%6f",n,area);
20         
21     }
22     public static double area(int n, int side){
23         double area = n*side*side/(4*Math.tan(Math.PI/n));
24         return area;
25         
26     }
27 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM