0
我的代碼中的所有內容終於看起來正確。我只是在棘手的事情上遇到麻煩。在BlueJ中使用Negative Infinity(垂直)斜率
我該如何編寫代碼,以便當我輸入兩點並且斜率爲-infinity
時,它被識別並且輸出爲Vertical
而非Negative 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
和垂直會是什麼樣子:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Vertical
而現在我的垂直輸出的樣子:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Negative Slope
以下是我現在的代碼:
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");
}
}