简体   繁体   English

无法将值从一个类传递到另一个类

[英]Having trouble passing values from one class to another

I have some java code that is supposed to be in the form of classes, take input as numbers, get the average, sum, and count of the numbers entered, and it seems to work for the most part except when actually trying to input values from the main method.我有一些应该以类的形式出现的 java 代码,将输入作为数字,获取输入数字的平均值、总和和计数,它似乎在大多数情况下都有效,除非实际尝试输入值从主要方法。 How can I adjust this so that the program is able to accept the values?我如何调整它以便程序能够接受这些值?

public class AverageCalculator {
    private int sumOfNumbers ;
    private int countOfNumbers ;

    AverageCalculator() { // no arg constructor
        sumOfNumbers = 0;
        countOfNumbers = 0;
    }

    void add(int newNum) { // adds a number to the average calculator
        this.sumOfNumbers = newNum + sumOfNumbers;
        countOfNumbers++;
    }

    int getSum() { // returns the sum of all the numbers added to average calculator

        return sumOfNumbers;
    }

    int getCount() {// returns the number of numbers added to the average calculator
        return countOfNumbers;
    }

    double getAverage() {// returns the average of all numbers added to average calculator
        double average = this.sumOfNumbers / this.countOfNumbers;
        return average;
    }
}

class AverageCalculatorMain {
    public static void main(String[] args) {
        AverageCalculator average = new AverageCalculator(4); //with one value
        AverageCalculator average = new AverageCalculator(3,4,5)//with three values
        System.out.println("The average is " + average.getAverage() + " the sum is " + average.getSum()
                + " the count of numbers is " + average.getCount());
    }
}

You haven't defined a constructor with arguments.您尚未定义带参数的构造函数。

Example constructor assuming you get only pass type int as arguments:示例构造函数假设您只获得传递类型int作为参数:

    AverageCalculator(int... args) {
        for(int i:args){
            sumOfNumbers+=i;
            countOfNumbers++;
        }
    }

Also, using these 2 lines in the no-arg constructor is redundant:此外,在无参数构造函数中使用这两行是多余的:

sumOfNumbers = 0;
countOfNumbers = 0;

Simply initialize them at the point of defining the variables.只需在定义变量时初始化它们。

Check out the code after the above mentioned fixes:在上述修复后查看代码:

public class AverageCalculator {
    private int sumOfNumbers=0;
    private int countOfNumbers=0;

    AverageCalculator() { // no arg constructor
    }

    AverageCalculator(int... args) {
        for(int i:args){
            sumOfNumbers+=i;
            countOfNumbers++;
        }
    }

    void add(int newNum) { // adds a number to the average calculator
        this.sumOfNumbers = newNum + sumOfNumbers;
        countOfNumbers++;
    }

    int getSum() { // returns the sum of all the numbers added to average calculator

        return sumOfNumbers;
    }

    int getCount() {// returns the number of numbers added to the average calculator
        return countOfNumbers;
    }

    double getAverage() {// returns the average of all numbers added to average calculator
        double average = this.sumOfNumbers / this.countOfNumbers;
        return average;
    }
}

class AverageCalculatorMain {
    public static void main(String[] args) {
        AverageCalculator average = new AverageCalculator(4); //with one value
        System.out.println("The average is " + average.getAverage() + " the sum is " + average.getSum()
                + " the count of numbers is " + average.getCount());
        AverageCalculator average2 = new AverageCalculator(3, 4, 5);//with three values
        System.out.println("The average is " + average2.getAverage() + " the sum is " + average2.getSum()
                + " the count of numbers is " + average2.getCount());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM