Write the average of the numbers typed, if they are even. Finish reading when user type zero(0)

Asked

Viewed 928 times

0

Ex) Write an algorithm that averages the numbers typed by the user if they are even. Finish reading if the user type zero.

I did so:

public static void main(String[] args){

    Scanner x = new Scanner (System.in);
    System.out.println("Digite um número: ");
    int numero = x.nextInt();

    while(numero < 0 | numero > 0){
        System.out.println("Digite outro número");

    /*
    *imagino que eu tenha que usar outra variável, mas isso prejudicaria
    *no inicio do meu while, que diz q a variável número não pode ser 0.
    */
        numero = x.nextInt();

    /*
     *existe maneira de guardar vários valores em uma mesma variável?(imagino 
     *que seja inviável).
     */

Could someone show me how the algorithm gets solved using while?

1 answer

1


A possible solution to your problem:

    Scanner x = new Scanner(System.in);
    System.out.println("Digite um número: ");
    int numero = x.nextInt();
    int somatorio = numero;
    int qnt = 1;
    while (numero != 0) {
        System.out.println("Digite outro número");
        numero = x.nextInt();
        if (numero != 0) {
            if(numero%2==0){
                somatorio += numero;
                qnt++;
            }
        }
    }
    System.out.println("Media = "+(somatorio/qnt));

With this the media will only be of the even numbers typed.

A second option :

   Scanner x = new Scanner(System.in);
   int numero = 0;
   int somatorio = 0;
   int qnt = 0;
   do {
        System.out.println("Digite um número: ");
        numero = x.nextInt();
        if (numero != 0) {
            if (numero % 2 == 0) {
                somatorio += numero;
                qnt++;
            }
         }
     } while (numero != 0);
     if (qnt != 0)
         System.out.println("Media = " + (somatorio / qnt));
     else
         System.out.println("Nenhum numero par digitado");
  • the way it was before editing until it worked, the only problem is that the average was not coming out with the right result. now after editing it’s averaging the last two numbers typed. I did just as you explained: while(number != 0){ System.out.println("Type another number"); number = x.nextDouble(); sum += number; qnt++; } System.out.println("Media ="+(somatorio/qnt)); } } but when I do for example, 10, 8, 6, 0. instead of the average coming out 8 that would be right, it comes out 4,666. Anyway thank you very much.

  • worked out, thank you!!!

  • just rolling a little problem, it’s ignoring the first value provided, when calculating the average.

  • Adjusted, now it’s working

  • perfect, thank you, now I’ll study your code kkk. hug

  • Only problem is if the first number is odd or zero.

  • I realized another answer with a solution that meets all options

  • if it is zero he for the program, but this is right, only problem is when the first is odd q the average of wrong, but it does not catch anything, was worth the strength ai man!

  • In need we are available =)

Show 4 more comments

Browser other questions tagged

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