【问题标题】:Am I making a mistake, because in declaring the variables?我是否犯了一个错误,因为在声明变量时?
【发布时间】:2020-12-05 16:01:11
【问题描述】:

我的代码有效。它给了我正确的结果,但我觉得我做错了,因为我声明x1y1x2y2 过于频繁(全局和本地)。我是吗?但是,如果我删除其中一个声明,它就不再起作用了。错误信息:

错误:找不到符号

也许有人可以向我解释,我应该如何在不经常声明x1y1x2y2 的情况下解决问题。

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

【问题讨论】:

  • 你想做什么?
  • 我正在尝试将欧几里得距离计算为函数和过程。并打印出结果。
  • "function" 和 "procedure" 在 Java 中没有特定的含义。 Java只有方法。你到底认为什么是“函数”或“过程”?你从哪里得到这些定义?
  • 你还没有全局声明任何东西。

标签: java distance declaration variable-declaration


【解决方案1】:

您可以简单地使用参数x1y1x2y2。否则,无论调用它的参数是什么,您的方法将始终返回相同的值。

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

【讨论】:

  • 不错!是的,这行得通,我想我明白为什么它更好了!
  • 这是一个很好的答案,我不知道为什么我会想到全局变量,但你的方式更干净(双关语不是故意的)
【解决方案2】:

您可以像这样简单地声明全局变量:

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

【讨论】:

    【解决方案3】:

    以下解决方案可以消除静态声明,代码看起来很整洁。

    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被称为Wrapper类,在这段代码中保存了Points信息。

    快乐编码。

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 2013-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      相关资源
      最近更新 更多