㈠ 如何用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(三個點)的正負性
直角用勾股定理
等腰或等邊更簡單