java三角形图形计算器怎么判断边?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

java三角形图形计算器怎么判断边?

蛮大人123 2016-06-08 18:02:23 1061
public class SanJiaoXing {
float length;
float high;
/**
 * @param args
 */
float zhouChang(){
    float zhou = 3*length;
    return zhou;
}
float mianJi(){
    float mian = (length*high)/2;
    return mian;
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    SanJiaoXing san = new SanJiaoXing();
    san.length = 6;
    san.high = 5;
    System.out.println("请输入三角形的长和高:");
    Scanner sc = new Scanner(System.in);
    san.length = sc.nextFloat();
    san.high = sc.nextFloat();//从键盘导入

    float zc = san.zhouChang();//通过对象调用周长计算面积
    System.out.println("周长"+zc);
    float mj = san.mianJi();
    System.out.println("面积"+mj);
}
}
Java
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:32:29
    public class Test 
    {
        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
            System.out.println("请输入三角形边长:");
            double a=scan.nextDouble();
            double b=scan.nextDouble();
            double c=scan.nextDouble();
            Triangle s=new Triangle();
            if((a+b)>c&&(a+c)>b&&(b+c)>a){
                s=new Triangle(a,b,c);
                System.out.println("三角形的三边分别为:"+a+"\t"+b+"\t"+c);
            }
            else{
                System.out.println("输入的三边不能形成三角形!");
                System.exit(0);
            }
            System.out.println("周长为:"+s.round());
            System.out.println("面积为:"+s.area());
        }
    }
     class Triangle{
        double a;
        double b;
        double c;
        public Triangle(){
        }
        public Triangle(double x,double y,double z){
            this.a=x;
            this.b=y;
            this.c=z;
        }
        public double round(){
            return a+b+c;
        }
        public double area(){
            double p=(this.a+this.b+this.c)/2.0;
            double x=Math.sqrt(p*(p-this.a)*(p-this.b)*(p-this.c));
            return x;
        }
    }
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题