triangle的代码:
package triangle;import java.text.DecimalFormat;public class Triangle {public Triangle() {}/** 判断三角形的类型*/public String triangleshape(int a, int b, int c) {if ((a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100)) {return "边的值不在范围内!";} else if ((a >= b + c) || (b >= a + c) || (c >= a + b)) {return "不构成三角形!";} else if (a == b || a == c || b == c) {if (a == b && a == c) {return "等边三角形!";} else {return "等腰三角形!";}} else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == b * b + a * a) {return "直角三角形";} else {return "一般三角形";}}/** 是否是三角行*/public boolean istriangle(int a, int b, int c) {if (("边的值不在范围内!".equals(triangleshape(a, b, c)) || ("不构成三角形!".equals(triangleshape(a, b, c))))) {return false;}return true;}/** 周长*/public int perimeter(int a, int b, int c) {if (istriangle(a, b, c)) {return a + b + c;} else {return 0;}}/** 面积*/public String erea(int a, int b, int c) {if (istriangle(a, b, c)) {double l = 0, s = 0;l = (a + b + c) * 0.5;s = Math.sqrt(l * (l - a) * (l - b) * (l - c));DecimalFormat df = new DecimalFormat("#.00");String erea = df.format(s);return erea;} else {return "";}}}
Version
package triangle;import java.text.DecimalFormat;public class Triangle {public Triangle() {}/** 判断三角形的类型*/public String triangleshape(int a, int b, int c) {if ((a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100)) {return "边的值不在范围内!";} else if ((a >= b + c) || (b >= a + c) || (c >= a + b)) {return "不构成三角形!";} else if (a == b || a == c || b == c) {if (a == b && a == c) {return "等边三角形!";} else {return "等腰三角形!";}} else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == b * b + a * a) {return "直角三角形";} else {return "一般三角形";}}/** 是否是三角行*/public boolean istriangle(int a, int b, int c) {if (("边的值不在范围内!".equals(triangleshape(a, b, c)) || ("不构成三角形!".equals(triangleshape(a, b, c))))) {return false;}return true;}/** 周长*/public int perimeter(int a, int b, int c) {if (istriangle(a, b, c)) {return a + b + c;} else {return 0;}}/** 面积*/public String erea(int a, int b, int c) {if (istriangle(a, b, c)) {double l = 0, s = 0;l = (a + b + c) * 0.5;s = Math.sqrt(l * (l - a) * (l - b) * (l - c));DecimalFormat df = new DecimalFormat("#.00");String erea = df.format(s);return erea;} else {return "";}}}
主函数MAIN
package triangle;import java.text.DecimalFormat;public class Triangle {public Triangle() {}/** 判断三角形的类型*/public String triangleshape(int a, int b, int c) {if ((a < 1 || a > 100) || (b < 1 || b > 100) || (c < 1 || c > 100)) {return "边的值不在范围内!";} else if ((a >= b + c) || (b >= a + c) || (c >= a + b)) {return "不构成三角形!";} else if (a == b || a == c || b == c) {if (a == b && a == c) {return "等边三角形!";} else {return "等腰三角形!";}} else if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == b * b + a * a) {return "直角三角形";} else {return "一般三角形";}}/** 是否是三角行*/public boolean istriangle(int a, int b, int c) {if (("边的值不在范围内!".equals(triangleshape(a, b, c)) || ("不构成三角形!".equals(triangleshape(a, b, c))))) {return false;}return true;}/** 周长*/public int perimeter(int a, int b, int c) {if (istriangle(a, b, c)) {return a + b + c;} else {return 0;}}/** 面积*/public String erea(int a, int b, int c) {if (istriangle(a, b, c)) {double l = 0, s = 0;l = (a + b + c) * 0.5;s = Math.sqrt(l * (l - a) * (l - b) * (l - c));DecimalFormat df = new DecimalFormat("#.00");String erea = df.format(s);return erea;} else {return "";}}}
结果截图: