有这样一个题:根据三角形的三条边长,判断其是直角、钝角,还是锐角三角形。程序的功能要求如下:1.先输入三角形的三条边长;2.判断能够构成三角形,构成三角形的条件是“任意两边之和大于第三边”,如果不能构成三角形则提示“不是三角形”,并且,提示用户继续输入;3.如果能构成三角形,判断三角形是何种三角形。如果三角形的任意一条边的平方(例如:a*a)等于其他两条边的和,则为直角三角形;如果任意一条边的平方大于其他两条边平方的和,则为钝角三角形,否则,为锐角三角形.
我用set,get的方法写出了这个题,但我看见书上是这么写的:public boolean xxsi (int a ,int b , int c)我没弄明白,希望各位大牛指点一下,怎么用boolean写出来。还有,我总感觉,在set,get的if选择结构里面,钝角三角形好像比锐角三角形更有优先权下面是我用set,get写的代码:package cn.puruidong.accp_13;
/**
 * @author PuRuidong
 */
/*
 *根据三角形的三条边长,判断其是直角,钝角,还是锐角三角形.
 *通常set 和 get是属性的存取器,一般称getter/setter. set表示设置值,get表示获取值.
 *public String getName() {}
 *public void setName(String name) {}
 */
import java.util.Scanner;
public class Triangle {
private double side1;//边长1
private double side2;//边长2
private double side3;//边长3

private String TriangleType  ;//三角形类型

public double getSide1(){ //使用get方法获取第一条边的值
Scanner input = new Scanner (System.in);
System.out.println("请输入第一条边(可包含小数):");
side1 = input.nextDouble();
return side1 ;
}

public void setSide1(double side1){ 
this.side1=side1;
}

public double getSide2(){ //获取第二条边的值
Scanner input = new Scanner (System.in);
System.out.println("请输入第二条边(可包含小数):");
side2 = input.nextDouble();
return side2;
}

public void setSide2(double side2){ 
this.side2 = side2 ;
}

public double getSide3(){ //获取第三条边的值
Scanner input = new Scanner (System.in);
System.out.println("请输入第三条边(可包含小数):");
side3 = input.nextDouble();
return side3;
}

public void setSide3 (double side3){
this.side3 = side3 ;
}

public String getTriangleType (){ //使用get获取相应字符串,并输出结果
return TriangleType ;
}
 
public void setTriangleType (String TriangleType){
this.TriangleType = TriangleType ;
}
    public static void main(String[] args) {
     Scanner input = new Scanner (System.in);
Triangle center = new Triangle ();//创建类
String choose = "" ; //选择
do {

double sideto1 = center.getSide1();//调用输出
double sideto2 = center.getSide2();//调用输出
double sideto3 = center.getSide3();//调用输出
if ( sideto1+sideto2<sideto3||sideto2+sideto3<sideto1||sideto1+sideto3<sideto2 ) { //判断,不能构成三角形,则直接退出循环
center.setTriangleType("这不能构成三角形");//设置值
     System.out.println(center.getTriangleType());//输出值
     System.out.println("请问是否继续(y/n):");
     System.out.println("********************");
     choose = input.next();
continue;
}
  if ((sideto1*sideto1)==(sideto3+sideto2)||(sideto2*sideto2)==(sideto1+sideto3)||(sideto3*sideto3)==(sideto1+sideto2)){//输出直角三角形
     center.setTriangleType("这是一个直角三角形");//设置值
     System.out.println(center.getTriangleType());//输出值
     }  else if (sideto1==sideto2&&sideto2==sideto3){//输出等边三角形
     center.setTriangleType("这是一个等边三角形");
     System.out.println(center.getTriangleType());
     }
     else if (sideto1*sideto1>sideto3*sideto3+sideto2*sideto2||sideto2*sideto2>sideto1*sideto1+sideto3*sideto3||sideto3*sideto3>sideto1*sideto1+sideto2*sideto2){//输出钝角三角形
     center.setTriangleType("这是一个钝角三角形");
     System.out.println(center.getTriangleType());
     } else if (sideto1*sideto1+sideto2*sideto2>sideto3||sideto1*sideto1+sideto3*sideto3>sideto2||sideto3*sideto3+sideto2+sideto2>sideto1) {
     center.setTriangleType("这是一个锐角三角形");
     System.out.println(center.getTriangleType());
     }
System.out.println("请问是否继续(y/n):");
choose = input.next();
System.out.println("********************");
}while (!"n".equals(choose));
System.out.println("程序退出,谢谢使用!");
}
}

解决方案 »

  1.   


    import java.util.Scanner;public class Triangle { public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    //三条边
    int a = 0;
    int b = 0;
    int c = 0;
    while (true) {
    a = sc.nextInt();
    b = sc.nextInt();
    c = sc.nextInt();
    //判断是否构成三角形,不能构成重新输入
    if (isTriangle(a, b, c)) {
    break;
    } else {
    System.out.println("输入的三边不能构成三角形,请重新输入!~");
    }
    }
    //判断输出输入的三角形为什么三角形
    showWhatTriangle(a,b,c);
    } /*
     * 判断输入的三边是否能构成三角形
     */
    public static boolean isTriangle(int a, int b, int c) { if (a > 0 && b > 0 && c > 0 && (a + b) > c && (a + c) > b
    && (b + c) > a) {
    return true;
    } return false;
    } /**
     * 判断输入的三边构成是吗三角形
     * @param a
     * @param b
     * @param c
     */
    public static void showWhatTriangle(int a, int b, int c) {
    if(a*a == b+c || b*b == a+c || c*c == a+b){
    System.out.println("直角三角形!~");
    }
    else if(a*a > b+c || b*b > a+c || c*c > a+b){
    System.out.println("钝角三角形!~");
    }else{
    System.out.println("锐角三角形!~");
    }
    }
    }
      

  2.   

    public boolean xxsi (int a ,int b , int c)这个方法应该就是判断是否是三角形的。public boolean xxsi (int a ,int b , int c){
    if ( a+b<c||b+c<a||a+c<b ) {
     return true;    
    } else
     return false;
    然后在main方法里面直接调用这个方法进行判断即可,另外建议楼主修改一下main方法。里面逻辑代码太多,建议把判断具体什么三角形的逻辑封装成一个方法,使类的层次更清晰。
      

  3.   

    1楼判断三角形是锐角直角钝角有错误
    public static void showWhatTriangle(int a, int b, int c) {
    int a2 = a * a;
    int b2 = b * b;
    int c2 = c * c;
            if(a2 == b2 + c2 || b2 == a2 + c2 || c2 == a2 + b2){
                System.out.println("直角三角形!~");
            } else if(a2 > b2 + c2 || b2 > a2 + c2 || c2 > a2 + b2){
                System.out.println("钝角三角形!~");
            } else {
                System.out.println("锐角三角形!~");
            }
        }