用java写:任意给定3个正实数abc作为三角形的三边,判断是否能组成三角形,若可以则输它的面积。

2024-11-17 23:49:10
推荐回答(1个)
回答1:

//@Author wangfeng import java.io.InputStreamReader; import java.io.BufferedReader; public class area { static InputStreamReader in; static BufferedReader breader = new BufferedReader(new InputStreamReader(System.in)); public static boolean judge(int a,int b,int c) { int[] array = new int[3]; array[0] = a; array[1] = b; array[2] = c; for (int i = 0;i < 2; i ++) { for (int j = 0; j array[j +1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1]= temp; } } if((array[0] + array[1]) > array[2]) { System.out.println("你输入的三条边能构成三角形"); return true; } else { System.out.println("你输入的三条边不能构成三角形"); return false; } } public static void calculate() { int array[] = new int [3]; double answer = 0.0; int p = 0; try{ for (int i = 0;i < 3; i ++) { System.out.println("请输入第" + (i+1) +"条边"); array[i] = Integer.parseInt(breader.readLine()); } }catch(Exception e){}; if(judge(array[0],array[1],array[2] ) == true) { p = (array[0]+ array[1]+array[2])/2; answer = Math.sqrt(p * (p - array[0]) * (p - array[1]) * (p - array[2])); System.out.println( "你输入的三条边构成的三角形面积为:" + answer); } } public static void main(String [] argu) { calculate(); } }