public class Taks02 { public static void main(String[] args) { double a, b, c,
s; Scanner sc = new Scanner(System.in); System.out.print("a = "); a = sc.
nextDouble(); System.out.print("b = "); b = sc.nextDouble(); System.out.print(
"c = "); c = sc.nextDouble(); // 调用三角形面积计算方法,传入实际参数a,b,c s = getTriangleArea(a,
b, c); // 实际参数(actual parameter),简称实参 // 根据返回值判断是否构成三角形 if (s != -1) { System.
out.println("s = " + s); } else { System.out.println("提示:构不成三角形!"); } } /** *
三角形面积计算方法 * * @param a 形式参数(formal parameter),简称形参 * @param b 形式参数(formal
parameter),简称形参 * @param c 形式参数(formal parameter),简称形参 * @return 三角形面积 */
private static double getTriangleArea(double a, double b, double c) { double s,
p; // 判断能否构成三角形 if (a + b > c && b + c > a && c + a > b) { p = (a + b + c) / 2;
s= Math.sqrt(p * (p - a) * (p - b) * (p - c)); // sqrt: square root } else { s =
-1; } return s; } }
结果: