JAVA 从键盘输入三个正数,判断这三个数能否构成三角形的三条边长;

2024-11-16 17:07:04
推荐回答(2个)
回答(1):

三角形判断:
package pack_triangle;
import java.io.*;
public class Triangle {
public static void main(String[] args)throws IOException {
double a,b,c;
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in2=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in3=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入边长a: ");
String line1=in1.readLine();
a=Double.parseDouble(line1);
System.out.print("请输入边长b: ");
String line2=in2.readLine();
b=Double.parseDouble(line2);
System.out.print("请输入边长c: ");
String line3=in3.readLine();
c=Double.parseDouble(line3);
if(a+b>c && a+c>b && b+c>a)
System.out.println("a、b、c可以构成三角形!");
else
System.out.println("a、b、c不可以构成三角形!");
}

}

成绩等级判断:
package pack_grade;
import java.io.*;
public class Grade {

public static void main(String[] args)throws IOException {
double x;
int a;
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入成绩X:");
String line1=in1.readLine();
x=Double.parseDouble(line1);
a=(x>=90 && x<=100 ? 1:0)
+(x>=80 && x<90 ? 2:0)
+(x>=70 && x<80 ? 3:0)
+(x>=60 && x<70 ? 4:0)
+(x<60 ? 5:0);
switch(a){
case 1:
System.out.println("优秀");
break;
case 2:
System.out.println("良好");
break;
case 3:
System.out.println("中等");
break;
case 4:
System.out.println("及格");
break;
case 5:
System.out.println("不及格");
break;
default:
break;
}
}

}
成绩等级判断这题我更改了一下,如当你输入89.5 这样的分数时也能判断相应的等级。

回答(2):

/*********************判断三角形**********************/
import java.io.*;
public class IsTria {
public boolean isTriangle(int l,int w,int h){
if(l+w>h)
return true;
else if(l+h>w)
return true;
else if(w+h>l)
return true;
else
return false;
}
public static void main(String[] args) {
IsTria it = new IsTria();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int l=0,w=0,h=0;
System.out.println("请输入三条边的长度:");
try {
l = Integer.parseInt(in.readLine());
w = Integer.parseInt(in.readLine());
h = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(it.isTriangle(l,w,h))
System.out.println("这三条边可以构成三角形");
else
System.out.println("这三条边不能构成三角形");
}
}
/*******************************************************/

/********************判定学生成绩等级***************************/
import java.io.*;
public class DifferClass {
public static void main(String[] args) {
System.out.println("请输入学生成绩:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int grade = 0;
String inclass = "";
try {
grade = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(grade/10){
case 10:
case 9: inclass = "优秀"; break;
case 8: inclass = "良好"; break;
case 7: inclass = "中等"; break;
case 6: inclass = "及格"; break;
default: inclass = "不及格"; break;
}
System.out.println("该成绩等级为:"+inclass);
}
}