JAVA程式怎麼寫?(判斷三角形類型&計算面積)

怎麼寫一個可以計算三角形面積和判斷是屬於哪一種三角形呢?(判斷等腰三角形 正三角形)

三角形的底和高(或三邊長)要可以讓使用者自行輸入的(不必有介面)

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    //Power by Eclipse//直角三角形的判斷式太長,必須當成一行來看。import java.util.Scanner;public class J_test { public static void main(String[] args) {  //=====MAIN STATEMENT=====//  Scanner input=new Scanner(System.in);  double Base,Hight,Side1,Side2,Side3;  System.out.printf("Input base of Triangle: ");  Base=input.nextInt();  System.out.printf("Input base of Triangle: ");  Hight=input.nextInt();  System.out.printf("Triangle Area: %f\n",getArea(Base,Hight));  System.out.printf("Input Side 1 of Triangle: ");  Side1=input.nextInt();  System.out.printf("Input Side 2 of Triangle: ");  Side2=input.nextInt();  System.out.printf("Input Side 3 of Triangle: ");  Side3=input.nextInt();  triangle_class(Side1,Side2,Side3);  //=====MAIN END=====// } public static double getArea(double base,double hight){  double area;  area=base*hight/2;  return area; } public static void triangle_class(double side1,double side2,double side3){  if(((Math.pow(side1,2)+Math.pow(side2,2))==Math.pow(side3,2))||((Math.pow(side2,2)+Math.pow(side3,2))==Math.pow(side1,2))||((Math.pow(side3,2)+Math.pow(side1,2))==Math.pow(side2,2))){   //直角   System.out.printf("Right triangle.\n");  }else if((side1==side2)&&(side2==side3)&&(side1==side3)){   //正三角   System.out.printf("Regular triangle\n");  }else if((side1==side2)||(side2==side3)||(side3==side1)){   //等腰   System.out.printf("Isosceles triangle.\n");  }else if(((side1+side2)>side3)&&((side2+side3)>side1)&&((side3+side1)>side2)){   //未知三角   System.out.printf("Unknown Triangle.\n");  }else{   //非三角   System.out.printf("It's not Triangle.\n");  } }}

  • 榮章
    Lv 4
    1 decade ago

    你是不是 po 錯版了...這裡是 C/C++ not Java...

Still have questions? Get your answers by asking now.