我不懂一行编程代码

时间:2019-09-29 08:11:29

标签: java

这是我为该类编写的代码,在其中声明了将在主类中调用的所有方法。我不知道怎么写,因此很难编写getAverageValue代码。

import java.util.Random;

public class MyCalculator
{
    private int min;
    private int max;
    private int sum;
    private int average;

    public int[] generatedRandomData()
    {
        int  inputData[]  =  new int[10];
        Random  generatedRandom  =  new Random();

        for(int i=0; i<10; i++)
        {
            inputData[i]  =  generatedRandom.nextInt(100);
        }
        return  inputData;
    }

    public int getMaxValue (int[] inputData)
    {
        int max;
        max = inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            if(max < inputData[i])
                max = inputData[i];
        }

        return max;
    }

    public int getMinValue (int[] inputData)
    {
        int min;
        min =inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            if(min > inputData[i])
                min = inputData[i];
        }

        return min;
    }

    public int getSumValue (int[] inputData)
    {
        int sum;
        sum =inputData[0];

        for (int i = 1; i <inputData.length; i++) 
        {
            sum+=inputData[i];
        }

        return sum;
    }

    public float getAverageValue (int[] inputData, int getSumValue)
    {
        int average= 0;

        for (int i = 1; i <inputData.length; i++) 
        {
            average = getSumValue/inputData.length;
        }

        return average;

    }

}

这是我为主班的代码:

public class MyApp 
{
    public static void main (String[] args)
    {
        MyCalculator calculator = new MyCalculator();
        int [] inputData = calculator.generatedRandomData();
        System.out.println("The generated values are: ");
        for (int value: inputData)
            System.out.print(value + ", ");
        System.out.println();
        System.out.println();

        System.out.println("Max Value: " + calculator.getMaxValue(inputData));
        System.out.println("Min Value: " + calculator.getMinValue(inputData));
        System.out.println("Sum Value: " + calculator.getSumValue(inputData));
        System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));

    }
}

编译此代码时,出现以下错误:

incompatible types: int cannot be converted to int[]
                System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));
                                                                                                    ^

Note: Some messages have been simplified;

3 个答案:

答案 0 :(得分:1)

这是您的原始声明:

System.out.println("Avg Value: " + calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length));

如果我们将其拆分为一次只能做一件事,我们将得到:

sum = calculator.getSumValue(inputData);
len = inputData.length;
average = calculator.getAverageValue(sum, len);
text = "Avg Value: " + average;
System.out.println(text);

两者都完成了同样的事情,但又分开了,它非常冗长

答案 1 :(得分:1)

getAverageValue的签名是

public float getAverageValue (int[] inputData, int getSumValue)

即该方法期望输入数据为int[]数组,然后为和的值。

您将其称为

calculator.getAverageValue(calculator.getSumValue(inputData), inputData.length)

即加上和的值(这是int-编译器抱怨的“不兼容类型”)和输入数。

正确的是:

calculator.getAverageValue(inputData, calculator.getSumValue(inputData))

请注意:您的实现可能不符合您的想象。参见Integer division: How do you produce a double?,然后考虑是否真的需要在那里循环。

答案 2 :(得分:0)

正确的代码:

System.out.println("Avg Value: " + 
    calculator.getAverageValue(inputData, calculator.getSumValue(inputData)));