java—我是不是犯了一个错误,因为在声明变量时?

vof42yt1  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(333)

我的代码有效。它给了我正确的结果,但我觉得我犯了一个错误,因为我声明 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));
    }
}
hmae6n7t

hmae6n7t1#

您可以简单地使用参数 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));
}
pokxtpni

pokxtpni2#

下面的解决方案可以消除静态声明和代码将看起来非常整洁。

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 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被称为 Package 类,它在代码中保存点信息。
快乐的编码。

qcbq4gxm

qcbq4gxm3#

您可以简单地声明如下全局变量:

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

相关问题