Area of Triangle, Java

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.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); } }
what are you inputting? Give us a sample run so we can see the problem a little more clearly (well formatted question, though)

以上就是Area of Triangle, Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准