2017-02-21 10 views
-5

とは異なる解決策を得ることがここでの練習です:"ポリゴンの面積を計算するJavaプログラムを作成します。" - www.w3resource.com

35. Write a Java program to compute the area of a polygon. Go to the editor 
Area of a polygon = (n*s^2)/(4*tan(π/n)) 
where n is n-sided polygon and s is the length of a side 
Input Data: 
Input the number of sides on the polygon: 7 
Input the length of one of the sides: 6 
Expected Output 

The area is: 130.82084798405722 

私のコードは、ウェブサイト上で127.30573435631248(長さ= 7、側面= 6) コードを返し、130.82084798405722(長さ= 7を返します。側面= 6)

私はトラブル鉱山は彼らのものと異なっている理由を見てを抱えている...

任意のアイデア?

は、ここに私のコードです:

public static void exercise35(){ 
    int number1 = integerInput(); //set to 7, length 
    int sides = integerInput(); //set to 6, sides 

    double area = (sides * (number1 * number1))/(4.0 * Math.tan((Math.PI/sides))); 

    System.out.println("The area of a polygon with " + sides + " sides of length " + number1 + " = " + area); 
} 

ここhttp://www.w3resource.com/java-exercises/basic/index.phpから溶液(運動#35)

import java.util.Scanner; 

public class Exercise35 { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     System.out.print("Input the number of sides on the polygon: "); 
     int ns = input.nextInt(); 
     System.out.print("Input the length of one of the sides: "); 
     double side = input.nextDouble(); 
     System.out.print("The area is: " + polygonArea(ns, side)+"\n"); 
    } 
    public static double polygonArea(int ns, double side) { 
     return (ns * (side * side))/(4.0 * Math.tan((Math.PI/ns))); 
    } 
} 
+0

ようこそStackOverflow。申し訳ありませんが、これはStackOverflowの仕組みではありません。フォームの質問_ "私のコードの束は、私のためにそれをデバッグしてください" _は話題外だと考えられています。詳細については、[help]にアクセスし、[ask]をお読みください。 –

+0

例の "sides"と "ns"変数のデータ型は何ですか? (解決策が簡単になるように騙されている) – stdunbar

+0

あなたは正しい古い電卓を使って誰が正しいかを試してみましたか? – rupps

答えて

0

は変更6と7だとあなたが望む答えを得るでしょう: )そして、将来、コードをデバッグして、何が起きているのかを見てみてください。

2

数式には、面と数が混在しています。

これは興味深いプログラミングの教訓を教えてくれます。「変数にわかりやすい名前を付けてください。

+0

ええ、名前を付けることもあります。いい視点ね。 – borowis

関連する問題