ssethzz :
So I'm supposed to make an overloaded program in Java. I have made my 2 methods on averaging the 6 numbers and the first 3 numbers. But I don't know how to store it into the parameters for the two methods. Here's my code so far:
Random number = new Random();
Scanner input = new Scanner(System.in);
int num;
int sum = 0;
for(int counter = 1; counter <=6; counter++){
num = 1 + number.nextInt(20);
System.out.printf("Random number #%s: %s%n",counter,num);
}
}
public static int avg (int a, int b, int c, int d, int e, int f){
return ((a+b+c+d+e+f)/6);
}
public static int avg (int a, int b, int c){
return((a+b+c)/3);
}
WJS :
I presume you aren't permitted to use arrays so just assign each to a variable.
num1 = 1 + number.nextInt(20);
num2 = 1 + number.nextInt(20);
num3 = 1 + number.nextInt(20);
// and so on for six numbers.
Then
int avgerage = avg(num1, num2, num3, ...);
Note that since you are not using doubles your average won't have a decimal.
Otherwise, place the numbers in an array.
You can also do it like this:
public int avg(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum/array.length;
}
If permitted, I would recommend changing your values from int
to double
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=218400&siteId=1