下面的代码不知哪里错了,无论输入什么数,结果都是“不能构成三角形”
import java.util.Scanner;
class Judge {
public boolean isTriangle(int a, int b, int c) {
boolean flag = false;
if ((a + b > c) && (a + c > b) && (b + c > a)) {
flag = true;
}
return flag;
}
public String shape(int a, int b, int c) {
String shape = "";
if (a == b && b == c) {
shape = "这是一个等边三角形";
} else if ((a * a + b * b == c * c) || (a * a + c * c == b * b)
|| (c * c + b * b == a * a)) {
shape = "这是一个直角三角形";
} else if ((a * a > b * b + c * c) || (b * b > c * c + a * a)
|| (c * c > b * b + a * a)) {
shape = "这是一个钝角三角形";
} else {
shape = "这是一个锐角三角形";
}
return shape;
}
}
public class Shape {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String d = "y";
while ("y".equals(d)) {
System.out.print("请输入第一边:");
int a = input.nextInt();
System.out.print("请输入第二边:");
int b = input.nextInt();
System.out.print("请输入第三边:");
int c = input.nextInt();
Judge show = new Judge();
if ("true".equals(show.isTriangle(a, b, c))) {
show.shape(a, b, c);
} else {
System.out.println("这不能构成三角形");
}
System.out.print("继续吗?(y/n):");
d = input.next();
}
System.out.println("谢谢使用!");
}
}
(Java)判断三角形的形状
答案:3 悬赏:30
解决时间 2021-12-20 09:42
- 提问者网友:但未伤到心
- 2021-12-19 15:52
最佳答案
- 二级知识专家网友:蜜罐小熊
- 2021-12-19 16:07
if ("true".equals(show.isTriangle(a, b, c))) {
改成
if (show.isTriangle(a, b, c)) {
if ((a + b > c) && (a + c > b) && (b + c > a)) {
改成
if (((a + b) > c) && ((a + c) > b) && ((b + c) > a)) {
你再试试
改成
if (show.isTriangle(a, b, c)) {
if ((a + b > c) && (a + c > b) && (b + c > a)) {
改成
if (((a + b) > c) && ((a + c) > b) && ((b + c) > a)) {
你再试试
全部回答
- 1楼网友:转身→时光静好
- 2021-12-19 17:52
class C{double a,b,d;public void getD(){//没有返回类型要用void if(a+b>d&&a+d>b&&b+d>a){ if(a*a==b*b+d*d||b*b==a*a+d*d||d*d==a*a+b*b){ System.out.printf("%-8.3f%-8.3f%-8.3f构成是直角三角形",a,b,d); } else if(a*a<b*b+d*d&&b*b<a*a+d*d&&d*d<a*a+b*b){ System.out.printf("%-8.3f%-8.3f%-8.3f构成锐角三角形",a,b,d); } else{ System.out.printf("%-8.3f%-8.3f%-8.3f构成钝角三角形",a,b,d); } } else{ System.out.printf("%f,%f,%f不能构成三角形",a,b,d); } }}import java.util.*;public class A{ public static void main (String args[ ]){ C c; c=new C(); Scanner reader=new Scanner(System.in); double a=0,b=0,d=0; System.out.print("输入边a:"); c.a=reader.nextDouble(); System.out.print("输入边b:"); c.b=reader.nextDouble(); System.out.print("输入边d:"); c.d=reader.nextDouble(); c.getD();}}
请采纳。
- 2楼网友:我们只是兮以城空
- 2021-12-19 17:45
首先确定输入的类型是使double 或者 float ,r如果不是,就throw 一个exception
例如void tri(double a, double b, double c) { //a, b, c 分别是散边长度
try{
if(任意两边之和大于第三边&&任意两边之差小于第三边){ // 这个地方不确定,使满足一个就可以还是要同时满足,所以都都写上了。保证没有错~~哈哈
if (a == b){
if(a==c) system.out.println("等边“)
else 输出等腰;
}
else 输出三角形;
}}
catch(e){
system.out.print("输入的不是数字“);//.
}
不知道这样写能不能帮到你。。。。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯