尝试确定三角形的面积。提示用户输入3个数字(双打)并计算程序中三角形的面积。该区域必须是至少3个小数位数。然而,该地区一直走向0.我做错了什么?
public static void main (String [] args) {
double sideA = 0.0;
double sideB = 0.0;
double sideC = 0.0;
int s = 1/2;
double area = 0.000;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter Side A: ");
sideA = scnr.nextInt();
System.out.println("Enter Side B: ");
sideB = scnr.nextInt();
System.out.println("Enter Side C: ");
sideC = scnr.nextInt();
DecimalFormat fmt = new DecimalFormat("0.###");
area = Math.sqrt((s * (s - sideA) * (s - sideB) * (s - sideC)));
System.out.println("The area of the triangle is: " + fmt.format(area));
return;
答案 0 :(得分:0)
这可能有所帮助,我已经接受了你的代码并对其进行了处理,这就是我的看法:
public static void main (String [] args) {
double base;
double height;
double s = 2;
double area;
Scanner scnr = new Scanner(System.in);
System.out.print("Enter length for the Base: ");
base = scnr.nextInt();
System.out.println("Enter Height of the triangle: ");
height = scnr.nextInt();
DecimalFormat fmt = new DecimalFormat("0.###");
area = (height*base) / s;
System.out.println("The area of the triangle is: " + fmt.format(area));
}
这将打印出157.5作为您的区域。你接近它,但是你将周边的公式和区域的公式结合起来,这就是为什么它没有用。