三角区,爪哇 [英] Area of Triangle, Java

查看:30
本文介绍了三角区,爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的家庭作业,我正在尝试构建一个计算三角形面积的应用程序.不太确定我哪里出错了,但我输入了三角形的长度,并希望根据 Heron 的公式显示正确的区域:sqrt (s(sa) (sb) (sc)).我得到的输出是 -0.0.代码如下:

I'm trying to build an app which computes the area of a triangle, as per my homework assignment. Not quite sure where I'm going wrong, but I input the lengths of the triangle and would like the proper area displayed according to Heron's formula: sqrt (s(s-a) (s-b) (s-c)). All I'm getting for output is -0.0. Here is the code:

import java.lang.Math;
public class Formula
{
    double area; double s;
    public double findArea(double sideA, double sideB, double sideC)
    { 
        s = 1/2 * (sideA + sideB + sideC);
        area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
        System.out.println("The area of the triangle is " + area);
        return area;
    }
}

然后我有另一个主要参数的文件

And then I have another file for the main args

import java.util.Scanner;

public class findTriangleArea {

    /**
     * @param args
     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Formula triangle = new Formula();
        double a,b,c;

        // input triangle lengths a, b, c 
        Scanner inputTriangle = new Scanner(System.in);
        System.out.println("Please enter triangle side a");
        a = inputTriangle.nextDouble();
        System.out.println("Please enter triangle side b");
        b = inputTriangle.nextDouble();
        System.out.println("Please enter triangle side c");
        c = inputTriangle.nextDouble();
        triangle.findArea(a, b, c);
    }
}

推荐答案

1/2 是在整数算术中计算的,所以与所有整数除法一样,它被截断——在这种情况下,<代码>0.只需编写 0.5 就可以了.

1/2 is being computed in integer arithmetic, so like with all integer division, it's truncated -- in this case, to 0. Just write 0.5 and you'll be fine.

这篇关于三角区,爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆