class Scanner problems

0
  Scanner lector = new Scanner(System.in);

        int numerosIngresados;
        int numeroMayor = 0;
        int numeroDiez = 0;
        int sumaImpares = 0;
        int contadorImpares = 0;
        int contadorNumeros = 0;


        for(int i = 0; i < 6; i++){
            System.out.println("Digite 6 numeros");
            numerosIngresados = lector.nextInt();

            if(numerosIngresados == 0){
                break;
            }else if(numerosIngresados > numeroMayor){
                numeroMayor = numerosIngresados;
            }else if(numerosIngresados == 10){
                numeroDiez++;

            }else if(numerosIngresados % 2 == 1){



            }
             numerosIngresados = contadorNumeros++;

        }
        System.out.println("El numero mayor es " + numeroMayor);
        System.out.println("El numero diez ha sido ingresado " + numeroDiez + " veces");
        System.out.println("La cantidad de numeros ingresados han sido: " + contadorNumeros);
        //System.out.println("El promedio de la suma de los numeros impares es " + sumaImpares);

When I print the number of times I have put the number 10, it tells me that I have entered it once less than what I have actually done. and how can I calculate the average of the odd numbers I entered?

    
asked by Tiago Viezzoli 06.10.2018 в 06:18
source

1 answer

1

I comment on the following, you do not print correctly the number of times you enter the number 10 because you are putting it in a else if where according to your flow that block of conditions never comes to be executed (at least not in the expected way)

it should be enough for you to change the else if() simply by a if in this way indistinct from the other conditional blocks always verify if you enter 10 numbers and execute the counter that you declared

Then your code should look like this

import java.util.*;
class Lector
{
    public static void main(String args[])
    {
        Scanner lector = new Scanner(System.in);

        int numerosIngresados;
        int numeroMayor = 0;
        int numeroDiez = 0;
        int sumaImpares = 0;
        int contadorImpares = 0;
        int contadorNumeros = 0;


        for(int i = 0; i < 6; i++){
            System.out.println("Digite 6 numeros");
            numerosIngresados = lector.nextInt();

            if(numerosIngresados == 0){
                break;
            }else if(numerosIngresados > numeroMayor){
                numeroMayor = numerosIngresados;
            } if(numerosIngresados == 10){
                numeroDiez++;

            }else if(numerosIngresados % 2 == 1){

            }
             numerosIngresados = contadorNumeros++;

        }
        System.out.println("El numero mayor es " + numeroMayor);
        System.out.println("El numero diez ha sido ingresado " + numeroDiez + " veces");
        System.out.println("La cantidad de numeros ingresados han sido: " + contadorNumeros);
        //System.out.println("El promedio de la suma de los numeros impares es " + sumaImpares);
    }
}

SAMPLE OF THE CODE WORKING

C:\Users\Alfred\Desktop\>javac Lector.java

C:\Users\Alfred\Desktop\>java Lector
Digite 6 numeros
10
Digite 6 numeros
10
Digite 6 numeros
5
Digite 6 numeros
8
Digite 6 numeros
7
Digite 6 numeros
4
El numero mayor es 10
El numero diez ha sido ingresado 2 veces
La cantidad de numeros ingresados han sido: 6
    
answered by 06.10.2018 / 06:38
source