Java:输入边长,判断是否为三角形

根据三角形的三条边长,判断其是直角、钝角,还是锐角三角形。程序的功能要求如下:

1.先输入三角形的三条边长;

2.判断能够构成三角形,构成三角形的条件是“任意两边之和大于第三边”,如果不能构成三角形则提示“不是三角形”,并且,提示用户继续输入;

3.如果能构成三角形,判断三角形是何种三角形。如果三角形的任意一条边的平方(例如:a*a)等于其他两条边的和,则为直角三角形;如果任意一条边的平方大于其他两条边平方的和,则为钝角三角形,否则,为锐角三角形.

我是用的set,get方法写的,关于书上的方法,我还没理解.不过,set,get方法比boolean好用一些。

1
public boolean xxsi (int a ,int b , int c)

参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cn.puruidong.accp_13;
/**
 * @author PuRuidong
 */
/*
 *根据三角形的三条边长,判断其是直角,钝角,还是锐角三角形.
 *通常set 和 get是属性的存取器,一般称getter/setter. set表示设置值,get表示获取值.
 *public String getName() {}
 *public void setName(String name) {}
 *来自:书本291页,第5题
 */
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 void show(){
        String choose = "" ; //选择
        String shape = "" ;//输出结果
        boolean zhi = true ;
        Scanner input = new Scanner (System.in);
        double sideto1 = getSide1();//调用输出
        double sideto2 = getSide2();//调用输出
        double sideto3 = getSide3();//调用输出
        if (( sideto1+sideto2<sideto3||sideto2+sideto3<sideto1||sideto1+sideto3<sideto2 )&&(sideto1<=0&&sideto2<=0&&side3<=0)) { //判断,不能构成三角形,则直接退出循环
            shape="这不能构成三角形,或者输入了小于0的数字.";//设置值
            System.out.println(shape);
            zhi = false ;
        }
        if (zhi){
             if ((sideto1*sideto1)==(sideto3+sideto2)||(sideto2*sideto2)==(sideto1+sideto3)||(sideto3*sideto3)==(sideto1+sideto2)){//输出直角三角形
                    shape="这是一个直角三角形";//设置值
                    System.out.println(shape);
            }  else if (sideto1==sideto2&&sideto2==sideto3){//输出等边三角形
                shape="这是一个等边三角形";//设置值
                System.out.println(shape);
            }
            else if (sideto1*sideto1>sideto3*sideto3+sideto2*sideto2||sideto2*sideto2>sideto1*sideto1+sideto3*sideto3||sideto3*sideto3>sideto1*sideto1+sideto2*sideto2){//输出钝角三角形
                shape="这是一个钝角三角形";//设置值
                System.out.println(shape);
            } else if (sideto1*sideto1+sideto2*sideto2>sideto3||sideto1*sideto1+sideto3*sideto3>sideto2||sideto3*sideto3+sideto2+sideto2>sideto1) {
                shape="这是一个锐角三角形";//设置值
                System.out.println(shape);
            }
        }
         
    }
   
    public static void main(String[] args) {
        Triangle center = new Triangle ();//创建类
        String choose = "" ; //选择
        Scanner input = new Scanner (System.in);
        do {
            center.show();
            System.out.println("请问是否继续(y/n):");
            choose = input.next();
            System.out.println("********************");
           
    }while (!"n".equals(choose));
        System.out.println("程序退出,谢谢使用!");
}
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据