大学学习之java篇
题目:编写一个程序。其中,三条边为三角型的属性,求三角形的面积和周长。 分别针对三条边为3,4,5和7,8,9的两个三角形进行测试。
使用工具:
Eclipse IDE for Java Developers - 2019-12
附上个人想法:
(1)判断三条边a b c是否能构成三角形。
(2)利用三角形周长和面积公式求得周长和面积。
代码如下:
package firstpack;
import java.util.Scanner;
public class text {
public static class triangle{
public void pd (int a,int b,int c){
int l,s;
if (a + b > c && a-b < c) {
l = a+b+c;
s = (int)(Math.sqrt((l/2)*(l/2-a)*(l/2-b)*(l/2-c)));
System.out.println("这三条边能构成三角形");
System.out.println("周长l="+l);
System.out.println("面积s="+s);
}
else {
System.out.printf("这三条边构不成三角形");}
}
}
public static void main(String args[]){
int x,y,z;
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一条边:");
x = scanner.nextInt();
System.out.print("请输入第二条边:");
y = scanner.nextInt();
System.out.print("请输入第三条边:");
z = scanner.nextInt();
triangle p =new triangle ();
p.pd(x,y,z);
}
}
运行结果: