java - 检查三角形是不等腰、等腰、等边还是不是三角形的JAVA程序
问题描述
我正在尝试编写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);
}
}
}
推荐阅读
- powershell - ConEmu:启动 PowerShell 的问题
- avfoundation - 如何一次下载多个视频?
- multithreading - Qt中术语“事件循环”wrt线程的含义是什么?
- algorithm - 如何找到连接不同联合之间不同节点的最佳方式?
- r - R Shiny:为长标签加宽 d3Tree
- dynamics-crm - getEventArgs().getSaveMode() 返回未定义以取消潜在客户资格
- git - 从 fork 中删除更改并同步到原点
- reactjs - 在 react/redux 中从 reducer 返回新状态的最佳实践是什么?
- php - 请求验证在 Symfony 中强加验证代码重复
- android - 使用带有 Appium 的混合 Android 智能手机扫描条码