Loop "for" counted odd number, even and averaging

2

I need to make a code that:

  • List a number of numbers
  • Inform if they are divisible by 3 and 5
  • If they are even or odd
  • And then take the separate mean of each (average of odd and average pairs)

I made a code, but when it does the average calculation, it is giving error.

Follow the code below:

public class Exe_04 {

    public static void main(String[] args) {

        Scanner ler = new Scanner(System.in);

        System.out.println("Informe a quantidade de numeros a serem listados.");
        int cp = ler.nextInt();

        float mediaPar, mediaImpar;
        int contPar = 0, contImpar = 0;

        int num[] = new int[cp];

        for (int i = 0; i < num.length; i++) {
            System.out.println("Digite o " + (i + 1) + "° numero: \n");
            num[i] = ler.nextInt();

            if ((num[i] % 3 == 0) && (num[i] % 5 == 0)) {
                System.out.println("Este numero é divisivel por 3 e 5.");
            }
            if (num[i] % 2 == 0) {
                System.out.println("Este numero é PAR.");
                contPar++;

            } else {
                System.out.println("Este numero é IMPAR.");
                contImpar++;
            }

        }
        mediaPar = contPar / num[cp];
        mediaImpar = contImpar / num[cp];

        System.out.println("A media dos numeros PARES é : " + mediaPar);
        System.out.println("A media dos numeros IMPARES é : " + mediaImpar);

    }

}
    
asked by anonymous 03.10.2017 / 04:23

1 answer

3

To calculate the mean you have to find the total and this was not being done, you were dividing it up for something meaningless.

I also improved some, for example I deleted the vector that was not actually being used.

There are other problems in this code, but for an exercise it is not bad, just do not think this is a good code for production use in serious application.

I was based on the statement, it can have errors there.

import java.util.*;

class Exe_04 {
    public static void main(String[] args) {
        Scanner ler = new Scanner(System.in);
        System.out.println("Informe a quantidade de numeros a serem listados.");
        int cp = ler.nextInt();
        int contPar = 0, contImpar = 0;
        int somaPar = 0, somaImpar = 0;
        for (int i = 0; i < cp; i++) {
            System.out.println("Digite o " + (i + 1) + "° numero: \n");
            int num = ler.nextInt();
            if (num % 3 == 0 && num % 5 == 0) {
                System.out.println("Este numero é divisivel por 3 e 5.");
            }
            if (num % 2 == 0) {
                System.out.println("Este numero é PAR.");
                contPar++;
                somaPar += num;
            } else {
                System.out.println("Este numero é IMPAR.");
                contImpar++;
                somaImpar += num;
            }
        }
        System.out.println("A media dos numeros PARES é : " + (float)somaPar / contPar);
        System.out.println("A media dos numeros IMPARES é : " + (float)somaImpar / contImpar);
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
03.10.2017 / 04:47