Java 蓝色距离
输出应如下所示:Java 蓝色距离,java,math,distance,bluej,Java,Math,Distance,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
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");
}
}
import java.util.Scanner;
公共类LineEvaluator
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入第一个点的x和y坐标:”);
double x1=input.nextDouble();
double y1=input.nextDouble();
System.out.print(“输入第二个点的x和y坐标:”);
double x2=input.nextDouble();
double y2=input.nextDouble();
双距离=数学sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
System.out.printf(“距离:%.3f”,距离);
双斜率=((y2-y1)/(x2-x1));
如果(坡度>0)
System.out.println(“正斜率”);
否则,如果(斜率<0)
System.out.println(“负斜率”);
否则,如果(斜率==0)
System.out.println(“水平”);
}
}
所以。。。首先,不要这样说int-to-double-查看文档,您会发现input.nextDouble()
存在,您可以直接使用它
e、 十:
此外,在距离公式中,您正在使用:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
如果您正在使用,请将您的替身命名为专有名称(x1
,x2
,等等)
使用int
时,它会截断。仔细阅读差异:
然后,您可以计算斜率,假设您将其存储在变量slope
中
当您试图找到具有无限斜率的斜率时,将出现以下错误:算术异常
您可以通过一个try-catch
来解决这个问题,在代码周围可以除以0,然后在这种情况下System.out.println(“垂直线”)
,或者您可以稍后计算并将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-else或switch语句计算坡度:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
希望你有这个想法。所以。。。首先,不要这样说int-to-double-查看文档,您会发现
input.nextDouble()
存在,您可以直接使用它
e、 十:
此外,在距离公式中,您正在使用:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
如果您正在使用,请将您的替身命名为专有名称(x1
,x2
,等等)
使用int
时,它会截断。仔细阅读差异:
然后,您可以计算斜率,假设您将其存储在变量slope
中
当您试图找到具有无限斜率的斜率时,将出现以下错误:算术异常
您可以通过一个try-catch
来解决这个问题,在代码周围可以除以0,然后在这种情况下System.out.println(“垂直线”)
,或者您可以稍后计算并将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-else或switch语句计算坡度:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
希望您能理解。删除javascript标记。稍微更新您的问题:您的问题到底出了什么问题?它是否无法编译,产生错误的输出…?@ben当我输入这些坐标时,我很难得到7.211的输出,而我得到的是7.000。我也不知道如何编写一个代码来告诉我斜率是水平的、垂直的、负的还是正的。用d替换x1,用f替换x2,以此类推。你已经成为整数除法的牺牲品,它会打印7.000,因为你将整数转换为一个双精度。对于你的另一个问题,请思考一下,将每一个都作为单词意味着什么。积极和消极都很容易。水平也是。垂直有点棘手,但不是太难。删除javascript标记。稍微更新一下你的问题:你的东西到底出了什么问题?它是否无法编译,产生错误的输出…?@ben当我输入这些坐标时,我很难得到7.211的输出,而我得到的是7.000。我也不知道如何编写一个代码来告诉我斜率是水平的、垂直的、负的还是正的。用d替换x1,用f替换x2,以此类推。你已经成为整数除法的牺牲品,它会打印7.000,因为你将整数转换为一个双精度。对于你的另一个问题,请思考一下,将每一个都作为单词意味着什么。积极和消极都很容易。水平也是。垂直有点棘手,但不是太难。哦,不,你是为他做的。你的回答是对的,但我希望能鼓励OP带着提示自己去那里。哦,好吧。无论如何,投票:P@BenKnoble我的错!我试图指出一些有用的资源供他阅读,但我知道人们从不阅读我所指的资源,所以我只告诉他发生了什么。我以后会离开的,没关系。这是一个意见和风格的问题。只要你不帮助那些“嘿,这是个问题,我什么都没有”的海报,你就没事了。这是一个很好的答案。嘿,谢谢你,真的很感激。我决定将其更改为
double x1=input.nextDouble()代码>就像你建议的那样。你在斜坡上把我弄丢了,但我会继续努力的。谢谢大家!@史蒂夫诺罗兹科,你在这方面需要什么帮助?哦,不,你是为他做的。你的回答是对的,但我希望能鼓励OP带着提示自己去那里。哦,好吧。无论如何,投票:P@BenKnoble我的错!我试图指出一些有用的资源供他阅读,但我知道人们从不阅读我所指的资源,所以我只告诉他发生了什么。我以后会离开的,没关系。这是一个意见和风格的问题。只要你不帮助“h”