无法想象如何使用方法重载

时间:2016-01-20 22:57:42

标签: java

这就是我所拥有的,我已经有一段时间了,似乎无法弄明白。如果有任何帮助,我将不胜感激。

public static double average(int NumOne,int NumTwo){
    double Sum;

    Sum =  (((double)NumOne + (double)NumTwo) / 2);
    return (Sum);
}
public static double average(int NumThree){
    double Sum = 0;

    Sum = + (((double)NumThree) / 3);
    return (Sum);
}

public static void main(String[] args) {
    Scanner Input = new Scanner(System.in);
    int Num1,Num2,Num3;

    System.out.print("Enter the first number:");
    Num1 = Input.nextInt();

    System.out.print("Enter the second number:");
    Num2 = Input.nextInt();

    System.out.print("Enter the third number:");
    Num3 = Input.nextInt();

    System.out.print(average(Num1, Num2));
    System.out.print(average(Num3));
}

}

1 个答案:

答案 0 :(得分:0)

public static double average(int NumOne,int NumTwo){
    double Sum;

    Sum =  (((double)NumOne + (double)NumTwo) / 2);
    return (Sum);
}

public static double average(int NumOne, int NumTwo, int NumThree){
    double Sum = 0;

    Sum =  (((double)NumOne + (double)NumTwo + (double)NumThree) / 3);
    return Sum;
}

然后在你的主要:

System.out.print(average(Num1, Num2));
System.out.print(average(Num1, Num2, Num3));