Java 如何在一个点中存储双精度?
我试图在java程序中创建几个点。这些点的坐标在一个文本文件中,我在其中扫描并逐条读取Java 如何在一个点中存储双精度?,java,point,Java,Point,我试图在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
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'
}
我得到错误“无法从双精度转换为一个点”,所以我需要知道如何修复此错误?允许我对点使用双精度坐标吗?这样做似乎更简单:
List<Point> points = new ArrayList<Point>(1000);
...
points.add(new Point(x, y));
列表点=新的ArrayList(1000);
...
点。添加(新点(x,y));
这样做似乎更简单:
List<Point> points = new ArrayList<Point>(1000);
...
points.add(new Point(x, y));
列表点=新的ArrayList(1000);
...
点。添加(新点(x,y));
点
类不会给出双精度。对于双精度,您需要使用类。考虑下面的代码作为例子。
static void pointTest() {
double x = 1.2;
double y = 3.4;
Point2D.Double pointDouble = new Point2D.Double(x, y);
System.out.println(pointDouble);
}
点
类不会给出双精度。对于双精度,您需要使用类。考虑下面的代码作为例子。
static void pointTest() {
double x = 1.2;
double y = 3.4;
Point2D.Double pointDouble = new Point2D.Double(x, y);
System.out.println(pointDouble);
}
这里有一个最小变化的解决方案:
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;
}
}
希望这对您有所帮助这里有一个最小更改的解决方案:
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;
}
}
希望这有助于使用本课程:
import org.springframework.data.geo.Point;
马文:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
org.springframework.boot
spring引导启动器数据jpa
使用此类:
import org.springframework.data.geo.Point;
马文:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
org.springframework.boot
spring引导启动器数据jpa
您在点
类中编写了哪些方法?有名字像setX
和setY
的吗?没有,我甚至没有点类。我不知道我需要一个。您能进一步解释一下吗?那么您正在从某个库的某处导入一个点
类?哪一个?在代码顶部是否有一个import
语句提到点
?是的,我有3个:import java.util.*;,导入java.io.*;,导入java.lang.reflect.Array;好的,这些导入都不会给你一个点
类。也许您的源代码所在的目录中有一个?或者您实际上也导入了java.awt.*
?它有一个名为Point
的类,但它不存储double
值。您在Point
类中编写了哪些方法?有名字像setX
和setY
的吗?没有,我甚至没有点类。我不知道我需要一个。您能进一步解释一下吗?那么您正在从某个库的某处导入一个点
类?哪一个?在代码顶部是否有一个import
语句提到点
?是的,我有3个:import java.util.*;,导入java.io.*;,导入java.lang.reflect.Array;好的,这些导入都不会给你一个点
类。也许您的源代码所在的目录中有一个?或者您实际上也导入了java.awt.*
?它有一个名为Point
的类,但是它不存储双值。如果你能展示这个点
类的结构,它会更容易指导。如果你不只是猜测他的点
类是什么以及它有什么构造函数,它会更容易指导他。如果你能展示这个点的结构,它会更容易指导点
类如果你不只是猜测他的点
类是什么以及它有什么构造函数,那么就更容易指导他了。使用哪个点
类?他已经在使用的点
类基于他提供的代码,显然有一个构造函数,它接受x和y双论据。你为什么不建议一个解决方案,这样我们就可以对它进行表决?使用哪个点
类?他已经在使用的点
类基于他提供的代码,显然有一个构造函数,它接受x和y双
参数。你为什么不提出一个解决方案,这样我们就可以投票表决呢?如果需要双精度,“Point2D.double”是个不错的选择。否则你可以施放双骰子使其浮动+1如果需要双精度,“Point2D.double”是一个不错的选择。否则你可以施放双骰子使其浮动+1你的答案与这个问题有何关联?你的答案与这个问题有何关联?