Method does not return correct value

Asked

Viewed 468 times

-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?

  • 2

    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 /

1 answer

4

One of the reasons is that in the method somar(int[] valores) there is an infinite loop. You are using i < valores.length as a condition, but forgot to make a i++ after the if.


I didn’t understand the program’s goal correctly, but it seems to me it’s to add up the odd numbers of a vector. In this case, the comparison valores[i] % 2 == 0.5, for integer numbers, will always result in false for the rest of the division by 2 (x % 2) is equal to 1 for odd or 0 for even.

So if you want to add the odd:

    while (i < valores.length) {
        if (valores[i] % 2 == 1) {
            soma = soma + valores[i];
        }
        i++;
    }

And for the pair:

    while (i < valores.length) {
        if (valores[i] % 2 == 0) {
            soma = soma + valores[i];
        }
        i++;
    }
  • I forgot to put the i++, corrected. I tried to take the condition of adding only odd, but only returns the value of the first index of the vector, if I put 1 and the other indices 3, it was to return 28, but returns only 1.

  • I didn’t understand. You removed the condition and now only adds the first element? I did the following: http://ideone.com/Rq06Sn. And it seems to be working.

  • I did some more tests and found that only the last value of the array is being displayed, for example, if I insert 1, 3, 5, 7, 9, 1, 3, 5, 7, 9, it will appear only the 9 integer; for some reason, it does not add, it just displays the value of the last position of the array.

  • Edit the ideone link I passed by changing the function somar for the way your.

  • http://ideone.com/lmFvqq

  • Here we added all odd valores.

  • The code I sent?

  • Yes. I typed (1,2,3,4,5,6,7,8,9,10) and added 25.

  • I’m using a treatment with Try/catch in another method that returns an Integer, does it have any problem returning an Integer to an int variable?

  • I think if you use Integer.intValue() no problem.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.