怎麼寫一個可以計算三角形面積和判斷是屬於哪一種三角形呢?(判斷等腰三角形 正三角形)
三角形的底和高(或三邊長)要可以讓使用者自行輸入的(不必有介面)
2006-07-28 08:44:40 · 2 個解答 · 發問者 曾蛋殼 1 in 電腦與網際網路 ➔ 程式設計
//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"); } }}
2006-07-28 14:20:41 · answer #1 · answered by Big_John-tw 7 · 0⤊ 0⤋
你是不是 po 錯版了...這裡是 C/C++ not Java...
2006-07-28 09:44:51 · answer #2 · answered by 榮章 4 · 0⤊ 0⤋