我犯错了吗,因为在声明变量时?
Am I making a mistake, because in declaring the variables?
我的代码有效。它给了我正确的结果,但我觉得我犯了一个错误,因为我过于频繁地(全局和本地)声明 x1
、y1
、x2
、y2
。我是吗?但是,如果我删除其中一个声明,它就不再起作用了。错误信息:
error: cannot find symbol
也许有人可以向我解释,我应该如何在不经常声明 x1
、y1
、x2
、y2
的情况下解决问题。
public class Distanz {
public static void main(String[] args) {
double d = 0;
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
berechneDistanzAlsProzedur(x1, x2, y1, y2);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(d));
}
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
x1 = 10;
y1 = 8;
x2 = 2;
y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
您可以简单地使用参数 x1
、y1
、x2
和 y2
。否则,您的方法将始终 return 相同的值,而不管调用时使用的参数如何。
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
您可以像这样简单地声明全局变量:
public class Distanz {
public static void main(String[] args) {
double d = 0;
berechneDistanzAlsProzedur();
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: "+berechneDistanzAlsFunktion(d));
}
static double x1 = 10;
static double y1 = 8;
static double x2 = 2;
static double y2 = 12;
public static void berechneDistanzAlsProzedur() {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
当然,我可能根本不知道你想要什么,我的回答可能完全没有用,但我觉得我回答得很好。 :D
下面的解决方案可以消除静态声明,代码看起来会很整洁。
public class Distanz {
public static void main(String[] args) {
Point point = new Point(10, 8, 2, 12);
berechneDistanzAlsProzedur(point);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(point));
}
public static void berechneDistanzAlsProzedur(Point point) {
double sqd_d = (point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2());
//This formula is same which you have used in berechneDistanzAlsFunktion method, you can eliminate berechneDistanzAlsProzedur function and directly call berechneDistanzAlsFunktion in main function
//You can pass different points to validate different result
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(Point point) {
return (Math.sqrt((point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2())));
}
}
你的观点 class 应该是这样的:
public class Point {
private final double x1;
private final double y1;
private final double x2;
private final double y2;
public Point(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double getX1() {
return x1;
}
public double getY1() {
return y1;
}
public double getX2() {
return x2;
}
public double getY2() {
return y2;
}
}
这里Point.java称为Wrapper class,它在这段代码中持有Points信息。
编码愉快。
我的代码有效。它给了我正确的结果,但我觉得我犯了一个错误,因为我过于频繁地(全局和本地)声明 x1
、y1
、x2
、y2
。我是吗?但是,如果我删除其中一个声明,它就不再起作用了。错误信息:
error: cannot find symbol
也许有人可以向我解释,我应该如何在不经常声明 x1
、y1
、x2
、y2
的情况下解决问题。
public class Distanz {
public static void main(String[] args) {
double d = 0;
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
berechneDistanzAlsProzedur(x1, x2, y1, y2);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(d));
}
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
x1 = 10;
y1 = 8;
x2 = 2;
y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double x1 = 10;
double y1 = 8;
double x2 = 2;
double y2 = 12;
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
您可以简单地使用参数 x1
、y1
、x2
和 y2
。否则,您的方法将始终 return 相同的值,而不管调用时使用的参数如何。
public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double x1, double x2, double y1, double y2) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
您可以像这样简单地声明全局变量:
public class Distanz {
public static void main(String[] args) {
double d = 0;
berechneDistanzAlsProzedur();
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: "+berechneDistanzAlsFunktion(d));
}
static double x1 = 10;
static double y1 = 8;
static double x2 = 2;
static double y2 = 12;
public static void berechneDistanzAlsProzedur() {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double d) {
double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
return (Math.sqrt(sqd_d));
}
}
当然,我可能根本不知道你想要什么,我的回答可能完全没有用,但我觉得我回答得很好。 :D
下面的解决方案可以消除静态声明,代码看起来会很整洁。
public class Distanz {
public static void main(String[] args) {
Point point = new Point(10, 8, 2, 12);
berechneDistanzAlsProzedur(point);
System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(point));
}
public static void berechneDistanzAlsProzedur(Point point) {
double sqd_d = (point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2());
//This formula is same which you have used in berechneDistanzAlsFunktion method, you can eliminate berechneDistanzAlsProzedur function and directly call berechneDistanzAlsFunktion in main function
//You can pass different points to validate different result
System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(Point point) {
return (Math.sqrt((point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2())));
}
}
你的观点 class 应该是这样的:
public class Point {
private final double x1;
private final double y1;
private final double x2;
private final double y2;
public Point(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double getX1() {
return x1;
}
public double getY1() {
return y1;
}
public double getX2() {
return x2;
}
public double getY2() {
return y2;
}
}
这里Point.java称为Wrapper class,它在这段代码中持有Points信息。
编码愉快。