1 public class MyTriangle 2 { 3 public static boolean isValid(double side1, double side2, double side3) 4 { 5 if((side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2)) 6 return true; 7 else 8 return false; 9 } 10 11 public static double area(double side1, double side2, double side3) 12 { 13 double s = (side1 + side2 + side3) / 2; 14 double areaSquare = s * (s - side1) * (s - side2) * (s -side3); 15 double areaSqrt = Math.sqrt(areaSquare); 16 return areaSqrt; 17 } 18 }
1 import java.util.Scanner; 2 3 public class TestMyTriangle 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter three sides: "); 10 double s1 = input.nextDouble(); 11 double s2 = input.nextDouble(); 12 double s3 = input.nextDouble(); 13 14 input.close(); 15 16 System.out.println(MyTriangle.isValid(s1, s2, s3)); 17 System.out.println(MyTriangle.area(s1, s2, s3)); 18 } 19 }
时间: 2024-12-11 00:25:26