我对 Java 很陌生,我似乎无法弄清楚为什么我的代码不起作用。理论上,它应该打印出 (0.0) 和我插入类中的任何内容之间的距离。有人可以帮帮我吗?
public class Homework61 {
double x;
double y;
Homework61(double q, double r) {
double x = q;
double y = r;
}
Homework61() {
x = 0.0;
y = 0.0;
}
public String toString() {
String a = "(" + x + "," + y + ")";
return a;
}
public double distanceFromOrigin()
{
double z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
return z;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Homework61 p = new Homework61();
System.out.println(p.toString() + " Is this far from origin: "
+ p.distanceFromOrigin());
p = new Homework61(3, 4);
p.x = 3;
p.y = 4;
System.out.println(p.toString() + " Is this far from origin: "
+ p.distanceFromOrigin());
}
}