qq_48586315
qq_48586315
采纳率100%
2020-06-25 14:28

本人初学java,哪位大佬能帮忙看看这个计算三角形面积的程序哪里错了吗?

已采纳

海伦公式求三角形面积:

public class Arith {
    public void  arith(double a, double b,double c) {

        if((a + b > c)&&(a + c > b)&&(b + c > a)&&(a - b < c)&&(
                a - c < b)&&(b - c < a)) {
            double z;
              double p =(a + b + c);
             z = Math.sqrt(p * (p - a) * (p - b) * (p - c));
             System.out.print(z);
        } 
    }
}


主程序:

import java.util.*;
public class Main {
    static double a;
    static double b;
    static double c;

    public static void main(String[] args) {
        Arith brith = new Arith();
        NewScanner newscanner =new NewScanner();
        Scanner scan = new Scanner(System.in);
        System.out.println("输入三角形三边");
        if(scan.hasNextDouble()) {
        a = scan.nextDouble();
        }

        System.out.println("第二条边");
        if(scan.hasNextDouble()) {
        b = scan.nextDouble();
        }

        System.out.println("第三条边");
        if(scan.hasNextDouble()) {
        a = scan.nextDouble();
        }

        brith.arith(a, b, c);
        // TODO Auto-generated method stub

    }

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • hjs218 Json-Huang 10月前

    main方法中c变量没有看到有赋值,

            System.out.println("第三条边");
            if(scan.hasNextDouble()) {
            a = scan.nextDouble();
            }
                    这里a = scan.nextDouble();应该改成c = scan.nextDouble();吧
    
    
    点赞 评论 复制链接分享