任务描述:
解决思路:
一个函数,带入输入的三个边长参数,条件满足计算其周长、面积,否则不计算
代码示例:
package a4_2024_07;
import java.util.Scanner;
public class j240721_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的3条边 a, b, c :");
int num01 = sc.nextInt();
int num02 = sc.nextInt();
int num03 = sc.nextInt();
boolean result = method(num01, num02, num03);
if (result) {
System.out.println("三条边能构成三角形");
double perimeter = num01 + num02 + num03;
double s = perimeter / 2;
double area = Math.sqrt(s * (s - num01) * (s - num02) * (s - num03));
System.out.printf("面积为:%.2f", area);
System.out.println();
System.out.println("周长为:" + perimeter);
} else {
System.out.println("三条边无法构成三角形");
}
}
// 判断是否能构成三角形, 任意两边之和大于第三边
private static boolean method(int num01, int num02, int num03) {
return num01 + num02 > num03 && num01 + num03 > num02 && num02 + num03 > num01;
}
}