static String getTriangleType(int a, int b, int c) { return getTriangleType(a, b, c); }优化
时间: 2024-05-05 18:17:47 浏览: 10
The given code is an infinite recursion as it calls the same method again and again without any break condition. To optimize the code, we can remove the recursion and directly check the triangle type using if-else statements. Here's an optimized version of the code:
```
static String getTriangleType(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return "Invalid";
} else if (a + b <= c || a + c <= b || b + c <= a) {
return "Not a triangle";
} else if (a == b && b == c) {
return "Equilateral";
} else if (a == b || b == c || c == a) {
return "Isosceles";
} else {
return "Scalene";
}
}
```
In this optimized code, we first check if any of the sides is less than or equal to zero, in which case the triangle is invalid. Then, we check if any two sides are less than or equal to the third side, in which case the triangle is not possible. If neither of these cases is true, we check if all sides are equal, in which case it is an equilateral triangle. If any two sides are equal, it is an isosceles triangle. Finally, if all sides are different, it is a scalene triangle.