重载方法问题

时间:2013-12-19 00:14:51

标签: java methods

我知道我这样做是完全错误的atm,我对编码很新,我知道无论我做什么都是错的。只是尝试。

创建两个重载方法,返回数组数组的平均值。一种方法是

public static double average (int[] array) 

其他方法标题是

public static double average (double[] array)

我的主要方法我想调用这两个,并打印平均值。第一个数组使用{1,2,3,4,5}来测试接受整数的平均方法和此数组{6.0,5.0,4.0,3.0,2.0,1.0}来测试接受双精度作为参数的平均方法。

现在我的代码可能看起来很糟糕。我真的不知道。

public class methodss 
{
    public static void main(String[] args) 
    {
        //invoke the average int method
        System.out.println("Average with int: " + average);
        //invoke the average double method
        System.out.println("Average with double: " + average);

        public static double average (int[] array)
        {
            int sum = 0, average = 0;

            array[5] = {1,2,3,4,5}
            for (int i = 0; i < 10; ++i){
                sum+=array[i];
                average = sum/5;
                return average;
            }
        }

        public static double average (double[] array)
        {
            int sum = 0, average2 = 0;

            array[6] = {6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
            for (int x = 0; x < 10; ++x){
                sum+=array[x];
                average = sum/6;
                return average;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个,

// Classes normally are named like proper nouns, so they start with a captial letter
public class Methods 
{
    // main is the entry point of the app.
    // all the args that are entered on the command line are passed in
    // as an array of strings.
    // the keyword static here means that this method belongs to this class
    // so all invokations are in terms of the class.
    // i.e. java Methods ... implicitly invokes Methods.main()
    public static void main(String[] args) 
    {
        //invoke the average int method
            System.out.println("Average with int: " + Methods.average(1, 2, 3, 4, 5));
        //invoke the average double method
            System.out.println("Average with double: " + Methods.average(1.0, 5.4, 2.3, 1.6);
    }

        /**
        * takes the average of an arbitrary amount of primitive ints
        * @param array a varg of ints, so that the caller can specify calls
        *   to the method like average(1) or average(1, 2) or average(new int[10]),
        *   makes calling this function in adhoc ways readable.
        * @returns a double representing the average of all the elements in the arguments
        **/
        public static double average (int... array)
        {
            int sum = 0;
            double average = 0.0;
            // loop through the array and stop when the iterator int, i
            // reaches the amount of elements in the array.
            // the amount of elements in the array can be read by the
            // special instance variable on array types called length.
            for (int i = 0; i < array.length; ++i){
                sum += array[i];
            }
            // so that these don't get truncated, inserting a 1.0 to make sure it's
            // a double. Otherwise the average of (1, 2) would be 1 instead of 1.5
            average = 1.0 * sum/array.length;
            return average;
        }


        /**
         * Same as average(int...) but with doubles
         **/ 
        public static double average (double... array)
        {

            double sum = 0.0, average = 0.0;

            for (int x = 0; x < array.length; ++x){
                sum += array[x];
            }
            average = sum/array.length;
            return average;
        }
}