Java方法 - 调用和定义

时间:2016-07-14 06:15:21

标签: java methods

试图找出如何将无效三角形类型编码到最后一个方法而不改变他所需的预写方法。

创建一个名为MyTriangle的类,它包含以下三种方法:

public static boolean isValid(double sidea, double sideb, double sidec)
public static double area(double sidea, double sideb, double sidec)
public static String triangleType(double a, double b, double c)

如果两个短边的总和大于最长边,则isValid方法返回true。三角形的三边长度被发送到此方法,但您可能不会认为它们是以任何特定顺序发送的。

area方法返回三角形的面积。考虑到三角形三边的长度,可以使用Heron公式计算三角形的面积(进行研究)。

triangleType方法返回以下字符串之一:" Equilateral"," Isosceles"," Scalene"," Invalid Triangle"。您可以确定三角形的类型,如果a,b和c按升序表示边,则必须首先确定三角形是否有效。如果它是有效的,如果a == c,则三角形是等边的。 Isosceles如果a == b或b == c。除此之外,还有Scalene。

在类中定义一个main方法,用于以任意顺序从用户三面输入三角形。然后主要方法应显示三角形的类型。如果三角形有效,则还必须输出三角形的区域。

public static void main(String[] args)
{
    // Declare all identifiers used in the main method
    int sidea, sideb, sidec;
    double area;
    boolean check;
    String type;

    Scanner input;
    input = new Scanner(System.in);

    System.out.println("Enter three sides for a triangle in any order: ");
    sidea = input.nextInt();
    sideb = input.nextInt();
    sidec = input.nextInt();

    check = isValid(sidea, sideb, sidec);

    double a = sidea;
    double b = sideb;
    double c = sidec;

    type = triangleType(a, b, c);

    if (check == true)
    {
        area = area(sidea, sideb, sidec);
        /*display the triangle's type and the area of the triangle. */
        System.out.print(type + ". " + area + ".");
    } else
    {
        System.out.print(type + ".");
        System.exit(0);
    }

}

/*Returns true if the sum of the two shorter sides is greater than the 
longest side.  Will sort the sides into ascending order first.*/
public static boolean isValid(double sidea, double sideb, double sidec)
{

    //Will return the boolean result.
    return (sidea + sideb > sidec && sideb + sidec > sidea && sidea + sidec > sideb);

}

/*Returns the area of the triangle given the lengths of the three sides of
the triangle*/
public static double area(double sidea, double sideb, double sidec)
{
    double s;
    double area;

    s = (sidea + sideb + sidec) / 2;
    area = Math.sqrt(s * (s - sidea) * (s - sideb) * (s - sidec));

    return area;
}

/*returns one of the following strings:  "Equilateral", "Isosceles", "Scalene",
"Invalid Triangle".  You can determine the triangle's type if a, b, and c 
represent the sides in ascending order, you must first determine if the triangle
is valid.  If it is valid, the triangle is equilateral if a == c.  Isosceles 
if a == b or b == c.  Scalene otherwise.*/
public static String triangleType(double a, double b, double c)
{

    if (c <= b)
    {
        double placeHolder = b;
        b = c;
        c = placeHolder;
    }// if sidec is less than  or equal to sideb, swap the numbers

    if (b <= a)
    {
        double placeHolder = a;
        a = b;
        b = placeHolder;
    }// if sideb is less than  or equal to sidea, swap the numbers

    if (c <= b)
    {
        double placeHolder = b;
        b = c;
        c = placeHolder;
    }// if sidec is less than or equal to sideb, swap the numbers
    //sidea is now the smallest, sideb is the middle, and sidec is the largest

    String type;

    if (a == c)
    {
        type = "Equilateral";
    } else if (a == b || b == c)
    {
        type = "Isosceles";
    } else
    {
        type = "Scalene";
    }

    return type;
}

2 个答案:

答案 0 :(得分:0)

你写了一个错误的陈述。     type = triangleType(a,b,c); //错误的陈述     type = triangleType(sidea,sideb,sidec); //更正

//there you go correct code.


public class Triangle {
    public static void main(String[] args)
    {
        // Declare all identifiers used in the main method
        int sidea, sideb, sidec;
        double area;
        boolean check;
        String type;

        Scanner input = new Scanner(System.in);

        System.out.println("Enter three sides for a triangle in any order: ");
        sidea = input.nextInt();
        sideb = input.nextInt();
        sidec = input.nextInt();

        check = isValid(sidea, sideb, sidec);

        type = triangleType(sidea, sideb, sidec);

        if (check == true){
        area = area(sidea,sideb,sidec);
        /*display the triangle's type and the area of the triangle. */
        System.out.print(type + ". " + area + ".");
        }
        else{
        System.out.print(type + ".");
        System.exit(0);
        }

    }

    public static boolean isValid(double sidea, double sideb, double sidec){

    if (sidea > sideb) {
        if(sidea >sidec){
            return ((sideb + sidec) > sidea);
        }
        else{
            return ((sidea + sideb) > sidec);
        }

    }
    else{
        if(sideb > sidec){
            return ((sidea + sidec) > sideb);
        }
        else{
            return ((sidea + sideb) > sidec);
        }
    }

    }

    public static double area(double sidea, double sideb, double sidec){
        double s;
        double area;

        s = (sidea + sideb + sidec)/2;
        area = Math.sqrt(s*(s - sidea)*(s - sideb)*(s - sidec));

        return area;
    }

    public static String triangleType(double a, double b, double c){
        String type;
        if (a == c){
            type = "Equilateral";
        } else if (a == b || b == c)
        {
            type = "Isosceles";
        } else
        {
            type = "Scalene";
        }

        return type;
    }

}

答案 1 :(得分:0)

您已经检查过isValid,为什么不使用它来确定类型:

if(!isValid(a,b,c){
 type = "Invalid triangle"}