我正在尝试在java程序中创建几个点。点的坐标位于文本文件中,我扫描并按编号读取。
double x = 0.0;
double y = 0.0;
Point origin = new Point(x, y);
Point[] points = new Point[1000]; // There are 1000 points in total, thus 2000 doubles, this array will be used to store all the points
Scanner keyboard = new Scanner(System.in);
String FileName = keyboard.nextLine();
Scanner linReader = new Scanner(new File(FileName));
while (linReader.hasNextDouble()) {
x = linReader.nextDouble();
y = linReader.nextDouble();
origin = (x, y); // error telling me 'cannot convert from double to point'
}
我收到错误"无法从双重转换为某个点"所以我需要知道如何修复此错误?我可以使用双打坐标作为分数吗?
答案 0 :(得分:2)
Point
等级不会给出双精度。对于双精度,您需要使用Point2D.Double类。请考虑以下代码作为示例。
static void pointTest() {
double x = 1.2;
double y = 3.4;
Point2D.Double pointDouble = new Point2D.Double(x, y);
System.out.println(pointDouble);
}
答案 1 :(得分:0)
public static void main(String args[]) {
//// Alternative 1
double x = 9.7;
double y = 8.6;
//import com.sun.javafx.geom.Point2D;
Point2D point2D = new Point2D();
point2D.x = (float) x; point2D.y= (float) y;
///// Alternative 2 (lose some accuracy)
//use Double object
Double x1 = 9.7;
Double y1 = 8.6;
//import java.awt.Point;
Point origin = new Point(x1.intValue(), y1.intValue());
}
答案 2 :(得分:0)
使用此类:
import org.springframework.data.geo.Point;
行家:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
答案 3 :(得分:-1)
似乎更直截了当地这样做:
List<Point> points = new ArrayList<Point>(1000);
...
points.add(new Point(x, y));
答案 4 :(得分:-1)
这是最低限度的变革解决方案:
double x = 0.0;
double y = 0.0;
Point origin;
Point[] points = new Point[1000]; // There are 1000 points in total, thus 2000 doubles, this array will be used to store all the points
Scanner keyboard = new Scanner(System.in);
String FileName = keyboard.nextLine();
Scanner linReader = new Scanner(new File(FileName));
while (linReader.hasNextDouble()) {
x = linReader.nextDouble();
y = linReader.nextDouble();
origin = new Point(x, y);
}
你也可以实现自己的Point类,就像这样
public class Point {
double x;
double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
}
希望这有帮助