Java 我是否犯了一个错误,因为在声明变量时?

Java 我是否犯了一个错误,因为在声明变量时?,java,distance,declaration,variable-declaration,Java,Distance,Declaration,Variable Declaration,我的代码有效。它给了我正确的结果,但我觉得我犯了一个错误,因为我太频繁地(全球和本地)声明x1,y1,x2,y2。是吗?但是,如果我删除其中一个声明,它将不再有效。错误消息: 错误:找不到符号 也许有人能向我解释一下,我应该如何解决这个问题而不经常声明x1,y1,x2,y2 public class Distanz { public static void main(String[] args) { double d = 0; double x1 = 10

我的代码有效。它给了我正确的结果,但我觉得我犯了一个错误,因为我太频繁地(全球和本地)声明
x1
y1
x2
y2
。是吗?但是,如果我删除其中一个声明,它将不再有效。错误消息:

错误:找不到符号

也许有人能向我解释一下,我应该如何解决这个问题而不经常声明
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
。否则,您的方法将始终返回相同的值,而不管调用它的参数是什么

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));
    }     
}
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;
    }
}

当然,我可能根本不知道你想要什么,我的回答可能完全没有用,但我认为我回答得很好下面的解决方案可以消除静态声明,代码看起来非常整洁

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())));
   }
}
您的Point类应该如下所示:

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));
    }     
}
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称为包装类,它在代码中保存点信息


愉快的编码。

你想做什么?我想把欧几里德距离作为一个函数和一个过程来计算。并打印出结果。“函数”和“过程”在Java中没有特定的含义。Java只有方法。你认为什么是“函数”或“过程”,你从哪里得到这些定义?你没有在全球范围内声明任何东西。好!是的,这很有效,我想我有点理解为什么它更好!这是一个很好的答案,我不知道为什么我的思维会转向全局变量,但你的方式更干净(双关语不是有意的)