Problem in entering data by keyboard with java

0

I was trying to do this program, the main problem is that it only lets me enter the value I want to give to numeroN , then it does not let me write by console and in the summation I get that is equal to zero, I wanted to know if someone can tell me what I'm wrong about.

// Hacer un programa que permita sumar los primeros numeros N pares.

int numeroN = 0;
int numero = 0;
int sumatoria = 0;

Scanner sc = new Scanner(System.in);
System.out.println("Ingrese el valor que desea asignarle al numero N : ");
numeroN = sc.nextInt();


for (int i = 1; i < numeroN; i++) {

    System.out.println("Ingrese un numero : ");

    if ((numero % 2) != 0) {

        numero = sc.nextInt();

    }

    else if ((numero % 2) == 0) {
        System.out.println("Error! El numero ingresado es par.");
    }

    sumatoria += numero;

}

System.out.println("La sumatoria es : " + sumatoria);
    
asked by computer96 05.08.2018 в 03:16
source

2 answers

1

for (int i = 1; i

answered by 05.08.2018 / 18:04
source
3

Your program does not do what you say in the description. You must review the steps used in your program.

Note that you initialize numero in 0 and only read the number when numero % 2 != 0 . I mean, you never read any number.

// inicializa el número
int numero = 0;

// si el número es impar (nunca)
if ((numero % 2) != 0) {
  // lee otro número.
  numero = sc.nextInt();
}
    
answered by 05.08.2018 в 03:42