㈠ 如何用java代码来做三角形的判断
public class Test7 {
public static String T(double a,double b,double c){
double tem = Math.max(a, b);
if(tem>c){
if(tem==a){
a = c;
}else {
b = c;
}
c = tem;
}
if(!(a+b>c&&Math.abs(a-b)<c)){
return "无法构成三角形";
}else if(a==b||a==c||b==c){
return "等腰三角形";
}else if(a*a+b*b==c*c){
return "直角三角形";
}else if(a*a+b*b<c*c){
return "锐角三角形";
}else return "钝角三角形";
}
public static void main(String[] args) {
System.out.println(Test7.T(11, 5, 12));
}
}
㈡ java 如何判断能否构成三角形
importjava.io.*;
classsanj{
publicstaticinta,b,c;
publicstaticvoidmain(Stringarg[])throwsIOException{
try{
BufferedReaderstdin=newBufferedReader(newInputStreamReader(System.in));//接收键值
System.out.println("输入三边值,每个值输入后回车");
System.out.println("请输入:");
a=Integer.valueOf(stdin.readLine());
b=Integer.valueOf(stdin.readLine());
c=Integer.valueOf(stdin.readLine());
}catch(IOExceptione){
System.out.println("出现异常!");
System.exit(0);
}
if(a+b<c||a+c<b||b+c<a){
System.out.println("你输入的不能构成三角形");
}
if(a==b||a==c||b==c)
{
if(a==b&&b==c)System.out.println("等边三角形");
elseSystem.out.println("等腰三角形");
}
if(a^2+b^2==c^2||a^2+c^2==b^2||b^2+c^2==z^2)
{
System.out.println("直角三角形");
if(a^2+b^2==c^2){
System.out.println("直角三角形面积"+a*b/2);
}
if(a^2+c^2==b^2){
System.out.println("直角三角形面积"+a*c/2);
}
if(b^2+c^2==z^2){
System.out.println("直角三角形面积"+c*b/2);
}
}else
System.out.println("一般三角形");
System.out.println(“y一般三角形周长是“+(a+b+c));}
}
㈢ java判断三角形类型
packagedemo;
importjava.util.Scanner;
publicclassTriangle
{
/**
*等腰
*/
publicstaticfinalintISOSCELES=1;
/**
*等边
*/
=2;
/**
*直角
*/
=4;
/**
*普通
*/
publicstaticfinalintGENERAL=6;
/**
*不构成三角形
*/
publicstaticfinalintNOTHING=7;
doublea;
doubleb;
doublec;
publicTriangle(doublea,doubleb,doublec)
{
this.a=a;
this.b=b;
this.c=c;
}
publicstaticintisWhichOne(Triangletriangle)
{
doublea=triangle.a;
doubleb=triangle.b;
doublec=triangle.c;
if(a+b>c&&a+c>b&&b+c>a&&Math.abs(a-b)<c&&Math.abs(a-c)<b&&Math.abs(b-c)<a)
{
if(a==b&&a==c)
{
returnTriangle.EQUILATERAL|Triangle.ISOSCELES;
}
elseif(a*a+b*b==c*c||a*a+c*c==b*b||c*c+b*b==a*a)
{
if(a==b||a==c||b==c)
{
returnTriangle.RIGHTANGLE|Triangle.ISOSCELES;
}
returnTriangle.RIGHTANGLE;
}
elseif(a==b||a==c||b==c)
{
returnTriangle.ISOSCELES;
}
else
{
returnTriangle.GENERAL;
}
}
else
{
returnTriangle.NOTHING;
}
}
publicstaticdoubleperimeter(Triangletriangle)
{
returntriangle.a+triangle.b+triangle.c;
}
publicstaticdoublearea(Triangletriangle)
{
doublea=triangle.a;
doubleb=triangle.b;
doublec=triangle.c;
return1.d/4*Math.sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a));
}
publicstaticvoidtranslate(Triangletriangle)
{
inttype=isWhichOne(triangle);
switch(type)
{
caseTriangle.ISOSCELES:
System.out.println("等腰三角形");
break;
case(Triangle.EQUILATERAL|Triangle.ISOSCELES):
System.out.println("等边三角形+等腰三角形");
break;
caseTriangle.GENERAL:
System.out.println("普通三角形");
break;
case(Triangle.RIGHTANGLE|Triangle.ISOSCELES):
System.out.println("直角三角形+等腰三角形");
break;
caseTriangle.RIGHTANGLE:
System.out.println("直角三角形");
break;
default:
System.out.println("不构成三角形");
break;
}
if(type!=Triangle.NOTHING)
{
System.out.println("周长是:"+perimeter(triangle));
System.out.println("面积是:"+area(triangle));
}
}
publicstaticvoidmain(String[]args)
{
System.out.println("输入构成三角形的三边长:(空格隔开) 退出按(n/N),否则继续:");
Stringerror="输入有误,请重新输入:";
Stringreg="^\d+\s+\d+\s+\d+$";
Scannerscanner=newScanner(System.in);
while(scanner.hasNextLine())
{
Stringline=scanner.nextLine().trim();
if("n".equalsIgnoreCase(line))
{
break;
}
if(!line.matches(reg))
{
System.err.println(error);
}
else
{
try
{
String[]input=line.split("\s+");
inta=Integer.parseInt(input[0]);
intb=Integer.parseInt(input[1]);
intc=Integer.parseInt(input[2]);
Triangletriangle=newTriangle(a,b,c);
translate(triangle);
}
catch(NumberFormatExceptione)
{
System.err.println(error);
}
System.out.println("退出按(n/N),否则继续:");
}
}
scanner.close();
}
}
㈣ 如何用java代码来做三角形的判断
只要能获得三角形的三边长度,就很容易了
假设三边长度为a、b、c
先判断是否为三角形:|a-b|<c<a+b(三个边)
钝角、锐角用余弦定理,判断cosA(三个点)的正负性
直角用勾股定理
等腰或等边更简单