1
I need to average 20 numbers and present all the numbers below average.
package pag1;
import java.util.Arrays;
import java.util.Scanner;
public class ex2 {
public static void main (String[] args){
Scanner x = new Scanner(System.in);
int soma = 0;
int posicao = 0;
double [] numeros = new double [20];
System.out.println("Digite 20 números para obter a média dos mesmos:");
while (posicao < numeros.length){
numeros [posicao] = x.nextDouble();
posicao++;
}
System.out.println(Arrays.toString(numeros));
for(int i=0; i<numeros.length; i++){
soma += numeros[i];
}
int media = soma / numeros.length;
System.out.println("soma: " + soma);
System.out.println(media);
}
}
We only missed the part about showing the numbers smaller than average, but I have no idea how.
Why not go through the list, compare each value with the average and, if it is smaller, display the number?
– Woss
I’m a beginner, how could I do this?
– João Laurent
The same way you did the rest of the code. What exactly is the difficulty? You made the code yourself?
– Woss
Yes, I’m trying, the difficulty is I don’t know which command to use to compare each item of the array with the final mean.
– João Laurent
Using the operator
<
: doif (x < y)
returns true if the value ofx
is less than that ofy
.– Woss