Java 如何优化这段代码
我开始了一门java课程,我被分配了一项任务,根据用户提供的顶点值检查两个三角形是否全等。 我不允许添加任何包(我添加的包除外),也不允许使用任何循环。 它工作正常,但是否有一个更短更有效的解决方案Java 如何优化这段代码,java,Java,我开始了一门java课程,我被分配了一项任务,根据用户提供的顶点值检查两个三角形是否全等。 我不允许添加任何包(我添加的包除外),也不允许使用任何循环。 它工作正常,但是否有一个更短更有效的解决方案 import java.util.Scanner; public class Congruent { public static void main (String [] args) { Scanner scan = new Scanner (System.in);
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");
}
}
可能更适合。由于IEEE浮点类型的不精确性,将双精度值与
=
进行比较不太可能产生有用的结果。相反,通过检查两个双精度值的差值大小来检查它们是否“几乎相等”。详细说明@VGR所说的内容,想象你有一个物理标尺,你正在用它测量边。即使测量两条相等的边,也永远不会得到完全相同的结果,这同样适用于处理浮点类型的函数,例如Math.sqrt()
。只需要一个微小的不精确性就可以阻止=
在应该返回true的时候返回true。相反,正如VGR所建议的,使用类似于Math.abs(b1-a2)<0.0001
的东西来代替b1==a2
。(VGR,如果有更好的方法,请纠正我。)如果不允许您使用循环(按照指令),那么您就无法更有效地编写此代码——这意味着代码行数明显减少的版本更短。格式如何?可以吗?另外,<0.0001的值到底是做什么的?我在谷歌搜索时没有看到函数描述中提到它……我想这意味着忽略了函数之外的差异?而且,我写它的方式似乎给出了正确的结果,即使它处理0后面有许多字符的距离。当处理(1,1)和(2,2)之间的距离时。