我正在尝试编写Java程序,以查看三角形是否为斜角,等腰,等边或非三角形。使用我使用的整数时,它应该不是三角形(1,1,30)。但是我一直在变斜角而不是三角形。任何帮助表示赞赏!谢谢!
public class Tri {
static void checkTriangle(int x, int y, int z)
{
// Check for equilateral triangle
if (x == y && y == z )
System.out.println("Equilateral Triangle");
// Check for isoceles triangle
else if (x == y || y == z || z == x )
System.out.println("Isoceles Triangle");
// Check for scalene triangle
else if (x != y || y!= z || z != x)
System.out.println("Scalene Triangle");
{
// Check for not a triangle
if (x + y < z || x + z < y || y + z > x)
System.out.println("Not a triangle");
}
}
public static void main(String[] args) {
{
int x = 1, y = 1, z = 30;
checkTriangle(x, y, z);
}
}
}
您应该首先检查非三角形情况。如下:
static void checkTriangle(int x, int y, int z)
{
// Check for not a triangle
if (x + y < z || x + z < y || y + z > x) {
System.out.println("Not a triangle");
} else {
// Check for equilateral triangle
if (x == y && y == z )
System.out.println("Equilateral Triangle");
// Check for isoceles triangle
else if (x == y || y == z || z == x )
System.out.println("Isoceles Triangle");
// Check for scalene triangle
else if (x != y || y!= z || z != x)
System.out.println("Scalene Triangle");
}
}
public static void main(String[] args) {
{
int x = 1, y = 1, z = 30;
checkTriangle(x, y, z);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句