如何优化这一小段代码

时间:2017-04-07 21:02:07

标签: java

我开始了一门java课程,我得到了一个任务,根据用户提供的顶点值检查两个三角形是否一致。 我不允许添加任何包(除了我做的那个),也不允许我使用任何循环。 它工作正常,但是有一个更短的更有效的解决方案吗?

import java.util.Scanner;
public class Congruent
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner (System.in);

        System.out.println("We will now calculate if two triangles are 
        congruent.");

        System.out.println("Enter the vertices values of the first triangle.");
        System.out.println("Please enter x1:");
        float x11 = scan.nextInt();
        System.out.println("Please enter y1:");
        float y11 = scan.nextInt();
        System.out.println("Please enter x2:");
        float x12 = scan.nextInt();
        System.out.println("Please enter y2:");
        float y12 = scan.nextInt();
        System.out.println("Please enter x3:");
        float x13 = scan.nextInt();
        System.out.println("Please enter y3:");
        float y13 = scan.nextInt();

        System.out.println("Enter the vertices values of the second triangle.");
        System.out.println("Please enter x1:");
        float x21 = scan.nextInt();
        System.out.println("Please enter y1:");
        float y21 = scan.nextInt();
        System.out.println("Please enter x2:");
        float x22 = scan.nextInt();
        System.out.println("Please enter y2:");
        float y22 = scan.nextInt();
        System.out.println("Please enter x3:");
        float x23 = scan.nextInt();
        System.out.println("Please enter y3:");
        float y23 = scan.nextInt();
        //calculating distances between the vertices of the first triangle and 
        placing them in variables
        double a1 = Math.sqrt((Math.pow((x11 - x12), 2)) + (Math.pow((y11 - 
        y12), 2)));
        double b1 = Math.sqrt((Math.pow((x12 - x13), 2)) + (Math.pow((y12 - 
        y13), 2)));
        double c1 = Math.sqrt((Math.pow((x13 - x11), 2)) + (Math.pow((y13 - 
        y11), 2)));
        //calculating distances between the vertices of the second triangle and 
        placing them in variables
        double a2 = Math.sqrt((Math.pow((x21 - x22), 2)) + (Math.pow((y21 - 
        y22), 2)));
        double b2 = Math.sqrt((Math.pow((x22 - x23), 2)) + (Math.pow((y22 - 
        y23), 2))); 
        double c2 = Math.sqrt((Math.pow((x23 - x21), 2)) + (Math.pow((y23 - 
        y21), 2)));

        System.out.println ("The first triangle is (" + x11 + ", " + y11 + ") (" 
        + x12 + ", " + y12 + ") (" + x13 + ", " + y13 + ").");
        System.out.println ("The lengths are " + a1 + "," + b1 + "," + c1 + 
        ".");
        System.out.println ("The second triangle is (" + x21 + ", " + y21 + ") 
        (" + x22 + ", " + y22 + ") (" + x23 + ", " + y23 + ").");
        System.out.println ("The lengths are " + a2 + "," + b2 + "," + c1 + 
        ".");
        // if else chain that compares distances between vertices to calculate 
        if the triangles are congruent on not and outputs the result to the user
        if      (a1 == a2 && b1 == b2 && c1 == c2)
        {
            System.out.println ("The triangles are congruent");
        }
        else if (a1 == a2 && b1 == c2 && c1 == b2) 
        {
            System.out.println ("The triangles are congruent");
        }
        else if (a1 == b2 && b1 == c2 && c1 == a2)
        {
            System.out.println ("The triangles are congruent");
        }
        else if (a1 == b2 && b1 == a2 && c1 == c2)
        {
            System.out.println ("The triangles are congruent");
        }
        else if (a1 == c2 && b1 == b2 && c1 == a2)
        {
            System.out.println ("The triangles are congruent");
        }
        else if (a1 == c2 && b1 == a2 && c1 == b2)
        {
            System.out.println ("The triangles are congruent");
        }
        else
            System.out.println ("The triangles are not congruent");
    }
}

0 个答案:

没有答案