[英]ArrayList x,y coordinate distance calculation
Can anyone help me figure out how to make this calculation work? 谁能帮我弄清楚如何进行此计算? I keep working myself in circles and I honestly don't know what to do. 我一直在圈子里工作,老实说我不知道该怎么办。 This is my current file that I'm working on: 这是我目前正在处理的文件:
import java.util.*;
import java.io.*;
public class Assignment_12 {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("name goes here");
System.out.println("You may quit at any time by typing \"Q\".");
ArrayList<Double> numbers = new ArrayList<Double>();
ArrayList<Double> numbers2 = new ArrayList<Double>();
System.out.println("Please enter an X coordinate: ");
//Point pnt = new Point();
while (input.hasNextDouble()) {
numbers.add(input.nextDouble());
System.out.println("Please enter a Y coordinate: ");
numbers2.add(input.nextDouble());
double x = (double) numbers.get(0);
double y = (double) numbers2.get(0);
Point pnt = new Point(x, y);
System.out.println(pnt.getX()); // just to make sure the "object created"?
System.out.println(pnt.getY()); // same as before, I think
System.out.println(pnt.distance(pnt.getX(), pnt.getY())); // how to calculate??
}
if (input.equals("Q")){
System.exit(0);
}
}
}
And this is the class that I'm just trying to figure out how to make the actual actual calculation work: 这是我正在尝试弄清楚如何使实际的实际计算工作起作用的类:
public class Point {
private double x;
private double y;
public Point(){
this.x = 0.0;
this.y = 0.0;
}
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double distance(Point pt){
return Math.sqrt((x - pt.x)*(x - pt.x) + (y - pt.y)*(y - pt.y));
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
I'm truly struggling on what exactly to do. 我真的在努力去做。 I seemingly created the pnt
object with x
and y
, but I don't necessarily understand how to make the calculated distance be available to print. 我似乎用x
和y
创建了pnt
对象,但我不一定理解如何使计算出的距离可用于打印。 I have tried System.out.println(pnt.distance(pnt.getX(), pnt.getY()));
我已经尝试过System.out.println(pnt.distance(pnt.getX(), pnt.getY()));
and other similar things, but I'm just stumped. 和其他类似的东西,但是我很困惑。 Any advice would be greatly appreciated! 任何建议将不胜感激!
Take a careful look at the signature of your method: 请仔细查看方法的签名:
double distance(Point pt)
// ⬑ single parameter of type Point
This method is called on a Point
, which you did correctly with point.distance(...
. But it takes one parameter, which also is of type Point
. You didn't pass a Point
though. Let me rephrase your code a little: 在Point
上调用了此方法,您可以使用point.distance(...
正确地执行point.distance(...
。但是它需要一个参数,该参数也是Point
类型的。虽然您没有传递Point
但是我还是稍微改一下代码:
double someX = pnt.getX();
double someY = pnt.getY();
pnt.distance(someX, someY)
This can't work, as you pass two double
, instead of one Point
. 这是行不通的,因为您传递了两个double
而不是一个Point
。 So what you can do is: 因此,您可以做的是:
pnt.distance(pnt)
// ⬑ single parameter of type Point
Compare this to what you did: 将此与您所做的比较:
pnt.distance(pnt.getX(), pnt.getY())
// ⬑ ⬑ two parameters of type double
But let me point out that this will return 0
, as you just calculated a Point
s distance to itself. 但是,我要指出的是,这将返回0
,因为您只是计算了Point
与自身的距离。 Try: 尝试:
Point otherPnt = new Point(13, 8.5);
System.out.println(pnt.distance(otherPnt));
Which will return at least a somewhat more interesting result. 这将至少返回一个更有趣的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.