BlueJ 中的距离
Distance in BlueJ
输出应如下所示:
Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope
如果您要查找距离,我已经使用代码应该是什么样子更新了 OP。
这是我的代码现在的样子:
import java.util.Scanner;
public class LineEvaluator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the x and y coordinates of the first point: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter the x and y coordinates of the second point: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.printf("Distance: %.3f", distance);
double slope = ((y2 - y1) / (x2 - x1));
if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
}
}
所以...首先不要像那样说 int to double - 进入文档,你会发现 input.nextDouble()
存在,你可以直接使用它。
e.x.:
int y2 = input.nextInt();
double g = y2;
to...
double g = input.nextDouble()
此外,在您的距离公式中,您使用的是:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
如果您正在使用它,请将您的替身命名为正确的名称(x1
、x2
、等等...)
当您使用 int
时,它会被截断。阅读差异:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
然后,你可以计算斜率,假设你将它存储在一个变量中slope
。
当您尝试寻找具有无限坡度的坡度时,您将收到此错误:ArithmeticException
。
您可以通过 try catch
解决此问题,围绕您可能除以 0 的代码,然后 System.out.println("Vertical Line")
在这种情况下,或者您可以稍后评估并保留 try catch
空白的。使用此:Double.isInfinite(double)
稍后评估。
尝试捕捉示例:
try{
slope = (y2-y1)/(x2-x1)//if it's divide by zero then we will get this error
}catch(ArithmeticException e){
//Take care of the error as we discussed.
}
使用 if-elses 或 switch 语句计算斜率:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
希望你明白了。
输出应如下所示:
Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope
如果您要查找距离,我已经使用代码应该是什么样子更新了 OP。
这是我的代码现在的样子:
import java.util.Scanner;
public class LineEvaluator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the x and y coordinates of the first point: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter the x and y coordinates of the second point: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.printf("Distance: %.3f", distance);
double slope = ((y2 - y1) / (x2 - x1));
if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
}
}
所以...首先不要像那样说 int to double - 进入文档,你会发现 input.nextDouble()
存在,你可以直接使用它。
e.x.:
int y2 = input.nextInt();
double g = y2;
to...
double g = input.nextDouble()
此外,在您的距离公式中,您使用的是:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
如果您正在使用它,请将您的替身命名为正确的名称(x1
、x2
、等等...)
当您使用 int
时,它会被截断。阅读差异:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
然后,你可以计算斜率,假设你将它存储在一个变量中slope
。
当您尝试寻找具有无限坡度的坡度时,您将收到此错误:ArithmeticException
。
您可以通过 try catch
解决此问题,围绕您可能除以 0 的代码,然后 System.out.println("Vertical Line")
在这种情况下,或者您可以稍后评估并保留 try catch
空白的。使用此:Double.isInfinite(double)
稍后评估。
尝试捕捉示例:
try{
slope = (y2-y1)/(x2-x1)//if it's divide by zero then we will get this error
}catch(ArithmeticException e){
//Take care of the error as we discussed.
}
使用 if-elses 或 switch 语句计算斜率:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
希望你明白了。