Java学习——判断三条边是否构成三角形以及构成直角、锐角、还是钝角三角形

public class Triangle {
    public boolean isTriangle(int a,int b, int c){
        boolean  flag=false;
           if ((a+b>c&&a+c>b&&c+b>a)&&(Math.abs(a-b)<c&&Math.abs(a-c)<b&&Math.abs(c-b)<a)){
              flag=true;
              shape(a,b,c);
          }else {
              System.out.println("这不能构成三角形。");
          }
        return flag;
   }
  public String shape(int a,int b, int c){
        String shape=null;
        if (a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
            shape="直角三角形";
            System.out.println("这是"+shape);
        }else if (a*a>b*b+c*c||b*b>a*a+c*c||c*c>a*a+b*b){
            shape="钝角三角形";
            System.out.println("这是"+shape);
        }else if (a*a<b*b+c*c||b*b<a*a+c*c||c*c<a*a+b*b){
            shape="锐角三角形";
            System.out.println("这是"+shape);
        }
        return shape;

  }
}
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        int a;
        int b;
        int c;
        String choice;
        Scanner input=new Scanner(System.in);
        do {
            Triangle tt=new Triangle();
            System.out.print("请输入第一条边:");
            a=input.nextInt();
            System.out.print("请输入第二条边:");
            b=input.nextInt();
            System.out.print("请输入第三条边:");
            c=input.nextInt();
            tt.isTriangle(a,b,c);
            System.out.print("继续吗?(y/n):");
            choice=input.next();
            if (choice.equals("n")){
                System.out.println("谢谢使用!");
                break;
            }
        }while (choice.equals("y"));


    }
}
    原文作者:最爱老詹LBJ
    原文地址: https://blog.csdn.net/weixin_43105912/article/details/88554966
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞