1.采用面向过程的思路
编程实现
public class CalculateTriangleArea {
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();
s=getTriangleArea(a,b,c);
if (s!=-1){
System.out.println("s="+s);
}else {
System.out.println("提示:构不成三角形");
}
}
public static double getTriangleArea(double a,double b,double c) {
double s,p;
if (a+b>c && a+c>b && c+b>a){
p=(a+b+c)/2;
s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
}else{
s=-1;
}
return s;
}
}
运行程序,查看结果
2.采用面向对象的思路
编程实现