2015-06-29 140 views
2

對不起,但它似乎我有點迷路。形狀計算器五角大樓和六邊形周邊和區域

我目前創建用於2D和3D形狀的Shape計算器和我似乎具有與在標題上述形狀的問題。

現在我已經約試圖利用這個代碼特定部分讓我五角大樓的地區,我在其他地方看到了這個工作,但無法弄清楚,爲什麼它甚至不會審查和比較後,在這裏工作我的代碼?我想有人可能會指出,如果這是解決問題的正確方法,或者我犯了一個錯誤,我看不到自己?一般需要第二個意見對不起。

double pen = scan.nextDouble(); 
 

 
double penPerm = pen * 5; 
 

 
double A1 = pen * Math.sqrt(5); 
 
double A2 = 5 + A1; 
 
double A3 = Math.sqrt(5 * A2); 
 
double PenA = (1.0/4.0) * A3 * Math.pow(pen, 2); 
 

 

 
System.out.println("Your Perimitre is :" + penPerm + "cm and your Area is :" + PenA + "cm Squared");

其他問題我有我應該如何去應對六角但說實話,上面五角大樓問題是我的主要關注點之前,我移動到了六邊形。

+0

我假設你的五邊形是規則的,並提出了一個解決方案,將工作的多邊形。 –

回答

0

對於正五邊形,你可以試試下面的代碼:

public static void main(String[] args) { 
    double side = 10; 
    double area = (1.0/4.0) * Math.sqrt(5*(5+2*Math.sqrt(5))) * Math.pow(side,2); 
    System.out.println("Your Perimitre is :" + 5*side + "cm and your Area is :" + area + "cm Squared"); 
} 

您還可以試試下面的代碼不帶邊(n)和邊緣尺寸(s)作爲輸入和計算面積的正多邊形:

 public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println(" Enter the number of sides in polygon"); 
     int n = input.nextInt(); 

     System.out.println(" Enter the distance between two points"); 
     double s = input.nextDouble(); 
     double area = (n * Math.pow(s, 2))/(4 * Math.tan(Math.PI/n)); 

     //Print result 
     System.out.println (" Area is " + area); 
     System.out.println (" Perimeter is " + s*n); 

    }