java从键盘输入三角形的三条边,判断它们能否构成三角形。若能构成三角形,指出是何种三角形?

2024-11-12 11:01:29
推荐回答(3个)
回答(1):

这样写

package com;
import java.util.Scanner;

public class W
{

public static void main(String[] args){
System.out.println("请输入三边以逗号隔开");
Scanner sc = new Scanner(System.in);
String location1 = sc.next();
String[] location1s = location1.split(",");
int x1 = Integer.parseInt(location1s[0]);
int y1 = Integer.parseInt(location1s[1]);
int z1 = Integer.parseInt(location1s[2]);

if((x1+y1>z1)&&(x1+z1>y1)&&(y1+z1>x1)){
System.out.println("能构成三角形");
}else{
System.out.println("不能构成三角形");
}

}
}

回答(2):

public static void main(String[] args) {

int i=0;
double sides[]={0L,0L,0L};

do{System.out.println("input the sides:");
Scanner sc=new Scanner(System.in);
for(i=0;i<3&&sc.hasNext();i++){
try{
sides[i]=Double.parseDouble(sc.next());
}catch(NumberFormatException e){
e.printStackTrace();
}
}
if(i!=3) System.out.println("input three sides please!");
}while(i!=3);
Arrays.sort(sides);
if((sides[0]+sides[1])>sides[2]){
String trianType="";
if(sides[0]*sides[0]+sides[1]*sides[1]>sides[2]*sides[2]){
if(sides[0]==sides[2]&&sides[0]==sides[1]){
trianType="equilateral ";
}else{
trianType="acute ";
}
}else if(sides[0]*sides[0]+sides[1]*sides[1] trianType="obtuse ";
}else{
trianType="right ";
}
if((sides[0]==sides[1]||sides[1]==sides[2])&&sides[0]!=sides[2])
trianType+="isosceles ";
System.out.println("those make a "+trianType+" triangle");
}else{
System.out.println("those don't make a triangle");
}

}

回答(3):

package com.hello;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class hello{
public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入长度X:");
String x=br.readLine();
System.out.println("请输入长度Y:");
String y=br.readLine();
System.out.println("请输入长度Z:");
String z=br.readLine();

int t;
int a = Integer.parseInt(x);
int b = Integer.parseInt(y);
int c = Integer.parseInt(z);

if(a>b){t=0; t=b; b=a; a=t;}
if(a>c){t=0; t=c; c=a; a=t;}
if(b>c){t=0; t=c; c=b; b=t;}

if(a+b >= c && a!= 0 && b!=0 && c!=0){
System.out.println("构成三解形");
if(a==b || a==c){
System.out.println("构成等腰三解形");
}
if(a==b && a==c){
System.out.println("构成等边三解形");
}
}
else
{
System.out.println("输入值不构成三解形");
}
}
catch(Exception e){}
}
}