gpt4 book ai didi

Java - 计算距离和到原点的距离 - 问题

转载 作者:行者123 更新时间:2023-12-01 18:56:45 24 4
gpt4 key购买 nike

我忘记了学校里的旧数学。我制作了一个 java 类来计算 x,y,z 和 origo 之间的距离。

但是在某个地方转错了方向,出不来……计算没有给出正确的值。你能帮我看看我做错了什么吗?这是距离计算错误。

import java.io.PrintWriter;
import java.util.Scanner;

public class U3point {
public static void main(String[] args) {

System.out.println("Values for x,y,z ");

Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);

double x;
double y;
double z;

String[] koordinater = null;
String rad = in.nextLine();

System.out.println("toString: ");

while (!rad.equals("")) {
koordinater = rad.split("\\s");
x = Double.parseDouble(koordinater[0]);
y = Double.parseDouble(koordinater[1]);
z = Double.parseDouble(koordinater[2]);

Point p = new Point(x, y, z);

out.println(p);

//call method for origo
double d = p.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);

// call method for distance
double d2 = p.getDistanceTo(x, y, z);
System.out.println("Distance: " + d2);

System.out.println("Exit program with enter or new values");
rad = in.nextLine();

}
}
}

class Point {

private double x;
private double y;
private double z;

public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}

public void setX(double x1) {
x = x1;
}

public void setY(double y1) {
y = y1;
}

public void setZ(double z1) {
z = z1;
}

public double[] getCoordinates() {
return new double[] { x, y, z };
}

public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}

public double getDistanceToOrigo()

{
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}

public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;

}
}

最佳答案

您计算到同一点的距离的问题,这里有一个适合您的方法:

import java.io.PrintWriter;
import java.util.Scanner;

public class U3point {
public static void main(String[] args) {

PrintWriter out = new PrintWriter(System.out, true);

Point p1 = generatePoint();
Point p2 = generatePoint();

out.println(p1);
out.println(p2);

// call method for origo double
double d = p1.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);

// call method for distance
double d2 = p1.getDistanceTo(p2.getX(), p2.getY(), p2.getZ());
System.out.println("Distance: " + d2);

System.out.println("The end!!");
}

public static Point generatePoint() {
System.out.println("Values for x,y,z ");
String[] koordinater = null;
Scanner in = new Scanner(System.in);
String rad = in.nextLine();

Point p = null;

koordinater = rad.split("\\s");

double x = Double.parseDouble(koordinater[0]);
double y = Double.parseDouble(koordinater[1]);
double z = Double.parseDouble(koordinater[2]);

p = new Point(x, y, z);

return p;

}

}

class Point {

private double x;
private double y;
private double z;

public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}

public void setX(double x1) {
x = x1;
}

public void setY(double y1) {
y = y1;
}

public void setZ(double z1) {
z = z1;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getZ() {
return z;
}

public double[] getCoordinates() {
return new double[] { x, y, z };
}

public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}

public double getDistanceToOrigo() {
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}

public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;

}
}

输出:

Values for x,y,z 
1 2 3
Values for x,y,z
4 5 6
x: =1.0 y:=2.0 z:=3.0
x: =4.0 y:=5.0 z:=6.0
Distance to Origo: 3.7416573867739413
Distance: 5.196152422706632
The end!!

关于Java - 计算距离和到原点的距离 - 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59667975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com