2016-07-14 11 views
-5

事前に書き込まれたメソッドを変更せずに無効な三角タイプを最後のメソッドにコード化する方法を理解しようとしています。 Javaメソッド - 呼び出しと定義


は、次の3つのメソッドが含まれていMyTriangleという名前のクラスを作成します:2つの短辺の合計が最も長い辺より大きい場合

public static boolean isValid(double sidea, double sideb, double sidec) 
public static double area(double sidea, double sideb, double sidec) 
public static String triangleType(double a, double b, double c) 

isValidメソッドがtrueを返します。三角形の3辺の長さはこのメソッドに送られますが、それらが特定の順序で送信されるとは想定できません。

エリアメソッドは、三角形のエリアを返します。三角形の3辺の長さが与えられると、三角形の面積は、Heronの公式(研究を行う)を用いて計算することができる。

triangleTypeメソッドは、 "Equilateral"、 "Isosceles"、 "Scalene"、 "Invalid Triangle"のいずれかの文字列を返します。 a、b、およびcが辺を昇順で表す場合は、三角形のタイプを判別できます。まず、三角形が有効かどうかを判断する必要があります。それが有効であれば、a == cなら三角形は正三法です。もしa == bまたはb == cなら二等辺三角形。そうでなければScalene。

三角形の3つの辺を任意の順序で入力するクラス内のメインメソッドを定義します。メインメソッドは三角形のタイプを表示します。三角形が有効な場合は、三角形の領域も出力する必要があります。


public static void main(String[] args) 
{ 
    // Declare all identifiers used in the main method 
    int sidea, sideb, sidec; 
    double area; 
    boolean check; 
    String type; 

    Scanner input; 
    input = new Scanner(System.in); 

    System.out.println("Enter three sides for a triangle in any order: "); 
    sidea = input.nextInt(); 
    sideb = input.nextInt(); 
    sidec = input.nextInt(); 

    check = isValid(sidea, sideb, sidec); 

    double a = sidea; 
    double b = sideb; 
    double c = sidec; 

    type = triangleType(a, b, c); 

    if (check == true) 
    { 
     area = area(sidea, sideb, sidec); 
     /*display the triangle's type and the area of the triangle. */ 
     System.out.print(type + ". " + area + "."); 
    } else 
    { 
     System.out.print(type + "."); 
     System.exit(0); 
    } 

} 

/*Returns true if the sum of the two shorter sides is greater than the 
longest side. Will sort the sides into ascending order first.*/ 
public static boolean isValid(double sidea, double sideb, double sidec) 
{ 

    //Will return the boolean result. 
    return (sidea + sideb > sidec && sideb + sidec > sidea && sidea + sidec > sideb); 

} 

/*Returns the area of the triangle given the lengths of the three sides of 
the triangle*/ 
public static double area(double sidea, double sideb, double sidec) 
{ 
    double s; 
    double area; 

    s = (sidea + sideb + sidec)/2; 
    area = Math.sqrt(s * (s - sidea) * (s - sideb) * (s - sidec)); 

    return area; 
} 

/*returns one of the following strings: "Equilateral", "Isosceles", "Scalene", 
"Invalid Triangle". You can determine the triangle's type if a, b, and c 
represent the sides in ascending order, you must first determine if the triangle 
is valid. If it is valid, the triangle is equilateral if a == c. Isosceles 
if a == b or b == c. Scalene otherwise.*/ 
public static String triangleType(double a, double b, double c) 
{ 

    if (c <= b) 
    { 
     double placeHolder = b; 
     b = c; 
     c = placeHolder; 
    }// if sidec is less than or equal to sideb, swap the numbers 

    if (b <= a) 
    { 
     double placeHolder = a; 
     a = b; 
     b = placeHolder; 
    }// if sideb is less than or equal to sidea, swap the numbers 

    if (c <= b) 
    { 
     double placeHolder = b; 
     b = c; 
     c = placeHolder; 
    }// if sidec is less than or equal to sideb, swap the numbers 
    //sidea is now the smallest, sideb is the middle, and sidec is the largest 

    String type; 

    if (a == c) 
    { 
     type = "Equilateral"; 
    } else if (a == b || b == c) 
    { 
     type = "Isosceles"; 
    } else 
    { 
     type = "Scalene"; 
    } 

    return type; 
} 
+0

この宿題を? – Sikorski

+0

はい、宿題です – Nikki

+3

ハムがハムスターと同じように、JavaはJavaScriptではありません。 – Jesper

答えて

0

あなたは間違った文を書いていました。 type = triangleType(a、b、c); //間違った文 type = triangleType(sidea、sideb、sidec); //補正

//there you go correct code. 


public class Triangle { 
    public static void main(String[] args) 
    { 
     // Declare all identifiers used in the main method 
     int sidea, sideb, sidec; 
     double area; 
     boolean check; 
     String type; 

     Scanner input = new Scanner(System.in); 

     System.out.println("Enter three sides for a triangle in any order: "); 
     sidea = input.nextInt(); 
     sideb = input.nextInt(); 
     sidec = input.nextInt(); 

     check = isValid(sidea, sideb, sidec); 

     type = triangleType(sidea, sideb, sidec); 

     if (check == true){ 
     area = area(sidea,sideb,sidec); 
     /*display the triangle's type and the area of the triangle. */ 
     System.out.print(type + ". " + area + "."); 
     } 
     else{ 
     System.out.print(type + "."); 
     System.exit(0); 
     } 

    } 

    public static boolean isValid(double sidea, double sideb, double sidec){ 

    if (sidea > sideb) { 
     if(sidea >sidec){ 
      return ((sideb + sidec) > sidea); 
     } 
     else{ 
      return ((sidea + sideb) > sidec); 
     } 

    } 
    else{ 
     if(sideb > sidec){ 
      return ((sidea + sidec) > sideb); 
     } 
     else{ 
      return ((sidea + sideb) > sidec); 
     } 
    } 

    } 

    public static double area(double sidea, double sideb, double sidec){ 
     double s; 
     double area; 

     s = (sidea + sideb + sidec)/2; 
     area = Math.sqrt(s*(s - sidea)*(s - sideb)*(s - sidec)); 

     return area; 
    } 

    public static String triangleType(double a, double b, double c){ 
     String type; 
     if (a == c){ 
      type = "Equilateral"; 
     } else if (a == b || b == c) 
     { 
      type = "Isosceles"; 
     } else 
     { 
      type = "Scalene"; 
     } 

     return type; 
    } 

} 
0

あなたは既にのisValidのチェックを持っているので、なぜいないタイプを把握するためにそれを使用する:

if(!isValid(a,b,c){ 
type = "Invalid triangle"} 
関連する問題