-1
I have the following code:
public class Main {
public static void Main (String[] args) {
Metodo R = new Metodo();
Scanner entrada = new Scanner(System.in);
int[] valores = new int[10];
int i = 0;
while (i < valores.length) {
System.out.println("Insira um número.");
valores[i] = entrada.nextInt();
i++;
} System.out.println(R.somar(valores));
}
}
public class Metodo {
public int somar(int[] valores) {
int soma = 0, i = 0;
while (i < valores.length) {
if (valores[i] % 2 == 0.5) {
soma = soma + valores[i];
}
i++;
}
return soma;
}
}
For some reason that I don’t know, it always returns 0, even putting only odd numbers, for example, if I put only the number 1 in all indexes of the array, it should return 10, but returns 0.
How can I fix it?
The % symbol in Java means rest, so as you will never have rest of . 5, it does not enter. If you want to use division, use the symbol /
– Roger Barretto