Loop "for" counted odd number, even and average

Asked

Viewed 2,003 times

2

I need to make a code that:

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

I wrote a code, but when it goes to perform the average calculation, it is giving error.

Follows code:

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);

    }

}

1 answer

3


To calculate the average need to find the total and this was not being done, was dividing by something meaningless.

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

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

I was based on the statement, there may be 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);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks Maniero for the help in the code and the tips, I’m new in java yet, but I will improve more my syntax and logic.

  • @Willgreco see the best way to say thank you on the [tour].

Browser other questions tagged

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