【发布时间】:2021-06-17 20:55:00
【问题描述】:
`java
//5. Write a program to read three sides of triangle and print area for valid data and to print “Invalid data”
// if either one side of the triangle is greater or equals to the sum of other two sides.
package com.company;
import java.util.Scanner;
import java.lang.Math;
public class EX_05 {
public static void main(String[] args) {
Scanner n = new Scanner(System.in);
System.out.println("Enter three sides of a triangle : ");
double a = n.nextDouble();
double b = n.nextDouble();
double c = n.nextDouble();
double s = (a+b+c)/2;
if((a>=(b+c)) || (b>=(a+c)) || (c>=(b+a)) ){
double area = Math.pow((s * (s - a) * (s - b) * (s - c)), 0.5);
System.out.println("The area of triangle is " + area + ".");
}
else {
System.out.println("Invalid data");
}
}
}
` 我想判断三角形的边是否有效来计算三角形的面积。
这是 IDE 显示的输出。
【问题讨论】:
-
您好,欢迎您。请edit您的问题包括您的源代码和输出作为文本而不是图片。
-
不相关但
Math有一个完美工作的sqrt方法 -
很明显,一个三角形(在欧几里得空间中)不能有边长为 10、5 和 2。
-
就像@Srikanth Chakravarthy 所说,您的条件必须从 > 更改为
-
请用这些边长给我画一个三角形,然后告诉我面积应该是多少。提示:en.wikipedia.org/wiki/Triangle_inequality