Home > Java > JavaBase > body text

Java method to determine whether it is a triangle (code attached)

Release: 2019-11-27 10:59:19
Original
13175 people have browsed it

Java method to determine whether it is a triangle (code attached)

Method to determine whether a triangle is a triangle in java: (Recommended: java video tutorial)

package Angle;
import java.util.Scanner;
public class Angle {
//判断是否能构成三角形
public static int JudgeAngle(int a,int b,int c)
{
if(a+b>c&&a+c>b&&b+c>a)
{
System.out.println("能够构成三角形!");
return 1;
}
else
{
System.out.println("不能构成三角形!");
return 0;
}

}
//判断是否能构成等腰三角形
public static int JudgeDangle(int a,int b,int c)
{
if(a==b||a==c||b==c)
{
if(a==b&&a==c)
{
System.out.println("该三角形是等边三角形!");
return 1;
}
else
{
System.out.println("该三角形是普通的等腰三角形!");
return 1;
}
}
else
{
return 0;
}

}
//判断是否是直角三角形
public static int JudgeRangle(int a,int b,int c)
{
int r1,r2,r3;
r1=a*a+b*b-c*c;
r2=a*a+c*c-b;
r3=b*b+c*c-a*a;
/*System.out.println(r1+r2+r3);*/
if(r1==0||r2==0||r3==0)
{
System.out.println("该三角形是直角三角形!");
return 1;
}
else
{
//System.exit(0);
return 0;
}

}

public static void main(String [] args)
{
int a;
int b;
int c;
System.out.println("请输入三角形的三边: ");
Scanner scanner=new Scanner(System.in);
a=scanner.nextInt();
b=scanner.nextInt();
c=scanner.nextInt();
//判断输入三边是否合法
if(a<0||a>200||b<0||b>200||c<0||c>200)
{
System.out.println("你输入的三边不合法!");
}
else
{
//判断是否能构成三角形
JudgeAngle(a,b,c);
//判断是否是等腰或等边三角形
JudgeDangle(a,b,c);
//判断是否是直角三角形
JudgeRangle(a,b,c);
//判断是一般三角形
System.out.println("该三角形是一般三角形!");
}

}
}
Copy after login

The above code determines by judging the length of the three sides Triangular or not.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java method to determine whether it is a triangle (code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template