首页 > 解决方案 > 检查三角形是不等腰、等腰、等边还是不是三角形的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); 
        } 
    } 
}

标签: javalogic

解决方案


您应该首先检查不是三角形条件。如下:

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); 
    } 
} 
}

推荐阅读