本帖最后由 廖力 于 2012-11-12 01:37 编辑
三角类Triangle里面有基本的方法;- package com.Lland.Triangle;
- import java.util.Arrays;
- /**
- *
- * @discription 判断是什么三角形
- * 构造方法public Triangle(int a, int b, int c)
- * 判断方法public String judgeTriangle()
- * 使用:
- * Triangle triangle = new Triangle(5, 3, 4);
- * System.out.println(triangle.judgeTriangle());
- *
- * @author 廖力
- * @time 2012年11月12日 01:29:35
- */
- public class Triangle {
- private int shorter, fit, longer;//三条边
- public Triangle(int a, int b, int c) {//构造方法,传进来三条边时候排序
- int[] array = new int[3];
- array[0] = a;
- array[1] = b;
- array[2] = c;
- Arrays.sort(array);
- shorter = array[0];
- fit = array[1];
- longer = array[2];
- }
- public String judgeTriangle() {//判断是什么三角形
- StringBuilder sb = new StringBuilder(info());
- int resultEdge = judgeEdge();
- int resultAngle = judgeAngle();
- if (isTriangle()) {
- sb.append("是");
- if (judgeEdge() == 6) {
- sb.append(valueOf(resultEdge));
- } else {
- sb.append(valueOf(resultAngle));
- sb.append(valueOf(resultEdge));
- }
- } else {
- sb.append("不是");
- }
- sb.append("三角形");
- return sb.toString();
- }
- private String valueOf(int value) {//判断结果转成文字
- String reslut = null;
- switch (value) {
- case 1:
- reslut = "锐角";
- break;
- case 2:
- reslut = "钝角";
- break;
- case 3:
- reslut = "直角";
- break;
- case 5:
- reslut = "等腰";
- break;
- case 6:
- reslut = "等边";
- break;
- default:
- reslut = "";
- break;
- }
- return reslut;
- }
- private int judgeEdge() {//按边判断
- int result = -1;
- if (shorter == longer) {
- result = 6;
- } else if (shorter == fit || fit == longer) {
- result = 5;
- }
- return result;
- }
- private int judgeAngle() {//按角判断
- int result = -1;
- int AB = shorter * shorter + fit * fit;
- int C = longer * longer;
- if (AB > C) {
- result = 1;
- } else if (AB < C) {
- result = 2;
- } else {
- result = 3;
- }
- return result;
- }
- private boolean isTriangle() {//判断能不能构成三角形
- int AB = shorter + fit;
- if (AB <= longer) {
- return false;
- }
- return true;
- }
- private String info() {//三角形信息
- StringBuilder sb = new StringBuilder();
- sb.append("三角形(").append(shorter).append(", ").append(fit).append(", ")
- .append(longer).append(")");
- return sb.toString();
- }
- }
复制代码 main方法类MainClass测试用- package com.Lland.Triangle;
- public class MainClass {
- public static void main(String[] args) {
- Triangle triangle = new Triangle(5, 3, 4);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(1, 8, 10);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(5, 12, 13);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(8, 8, 8);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(6, 6, 8);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(3, 3, 5);
- System.out.println(triangle.judgeTriangle());
- triangle = new Triangle(109, 108, 21);
- System.out.println(triangle.judgeTriangle());
- }
- }
复制代码 运行结果:
三角形(3, 4, 5)是直角三角形
三角形(1, 8, 10)不是三角形
三角形(5, 12, 13)是直角三角形
三角形(8, 8, 8)是等边三角形
三角形(6, 6, 8)是锐角等腰三角形
三角形(3, 3, 5)是钝角等腰三角形
三角形(21, 108, 109)是锐角三角形
利用三角形特性:
设较短的边为a,较长的边为c,长度居中的边为b
1.a + b > c
2.a^2 + b^2 > c^2, 锐角三角形
a^2 + b^2 = c^2, 直角三角形
a^2 + b^2 < c^2, 钝角三角形
3.a = c,等边
a != c 情况下
a = b,钝角等腰
b = c,锐角等腰
|