我无法获得要在 Java 中打印的所有节点的平均值

I can't get the average of all the nodes to print in Java

我正在摆弄 Java 中的节点,以便更好地适应它们。 我做了一个简单的节点Class。 然后在我的主 class 中,我试图获得所有节点的平均值。 当我尝试打印它时没有错误,我有多种方法可以测试输出但是当我 运行 它除了平均值之外一切都打印正常。 我将 Eclipse 用作 IDE 并且没有显示错误但是当我 运行 它编译的代码和 运行s 没有问题但我仍然无法让它打印平均。

public class Node {
    double a;
    Node next;

    Node() {
    
    }

    Node(double i){
        this.a = i;
    }


}

public class LLPractice {
    public static void main(String [] args) {
        Node n1 = new Node(60.1);
        Node n2 = new Node(30.2);
        Node n3 = new Node(40.4);
        Node n4 = new Node(50.5);
    
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
    
        System.out.println();
        traverse(n1);
    
        System.out.println();
        double d = sumup(n1);
        System.out.println("Summation: " + d);
    
        // Find the min and max and average numbers in the linked list
        double x = getMin(n1);
        System.out.println("Minimum: " + x);
    
        double y = getMax(n1);
        System.out.println("Max: " + y);
    
        double z = getAvg(n1);
        System.out.println("Average: " + z);
    
    }

    static double getAvg(Node head){
        double total = 0;
        int numOfNodes = 0;
        Node temp = head;
        while(temp != null) {
            temp.a += total;
            numOfNodes++;
        }
        double avg = total/numOfNodes;
        return avg;
    }

    static double getMax(Node head) {
        double max = head.a;
        Node temp = head;
        while(temp != null) {
            if(max < temp.a) {
                max = temp.a;
            }
            temp = temp.next;
        }
        return max;
    }

    static double getMin(Node head) {
        double min = head.a;
        Node temp = head;
        while(temp != null) {
            if(min > temp.a) {
                min = temp.a;
            }
            temp = temp.next;
        }
        return min;
    }

    static double sumup(Node head) {
        double sum = 0;
        Node temp = head;
        while(temp != null) {
            sum += temp.a;
            temp = temp.next;
        }
        return sum;
    }

    static void traverse(Node head){
        Node temp = head;
    
        while(temp != null) {
            System.out.println(temp.a);
            temp = temp.next;
        }
    }
}

总数不变。您在 temp.a

中的总结
   static double getAvg(Node head){
        double total = 0;
        int numOfNodes = 0;
        Node temp = head;
        while(temp != null) {
            temp.a += total; // <-- problem is here. reverse these and make
                             // sure you iterate thru the nodes.
            numOfNodes++;
        }
        double avg = total/numOfNodes; 
        return avg;
    }