Display a series of even and odd numbers in java

0

Eh made a program in java that makes the user enter a total number of numbers to evaluate, if you enter 4, you will have to enter 4 times any number, and then verify if it is even or not, one by one, but , when I try to deploy the succession of the pairs and inpairs, I only display the last value that I enter. How can I make the display something like this? Enter the number of numbers to be evaluated:

4

Enter num1:

2

Enter num2:

4

enter num3:

3

enter num4:

5

The even numbers are:

2, 4

The odd numbers are:

3, 5

    int li, num;
    System.out.println("Ingrese la cantidad de nuemros a evaluar: ");
    Scanner sc = new Scanner(System.in);
    li= sc.nextInt();
                    int np = 0;
                    int nip=05;

    for(int i=1; i<=li; i++)
    {
        System.out.println("Ingrese su numero #" + i);
        num = sc.nextInt();
        if(num%2==0)
        {
            np = num;
        }
        else
        {
            nip= num;
        }
    }
    System.out.println("Los numeros pares son: ");
    System.out.println(np);
    System.out.println("Los numeros inpares pares son: "+ nip);
    
asked by Axwell Duarte 18.10.2018 в 05:14
source

2 answers

0

You could save the numbers in different arrays, depending on whether the number is even or odd, what happens is that you save the numbers in a single variable and not save the others, here is an example of a possible solution:

    ArrayList<Integer> pares = new ArrayList<>();
    ArrayList<Integer> inpares = new ArrayList<>();
    int li, num;
    System.out.println("Ingrese la cantidad de nuemros a evaluar: ");
    Scanner sc = new Scanner(System.in);
    li = sc.nextInt();

    for (int i = 1; i <= li; i++) {
        System.out.println("Ingrese su numero #" + i);
        num = sc.nextInt();
        if (num % 2 == 0) {
            pares.add(num);
        } else {
            inpares.add(num);
        }
    }
    System.out.println("Los numeros pares son: ");
    for(Integer n : pares){ System.out.print(n); }
    System.out.println("Los numeros inpares pares son: ");
    for(Integer n : inpares){ System.out.print(n); }
  • As you see I define two ArrayList of Integer type, to contain numbers
  • Within the cycle I choose where to save the number, using the .add(...) method of the ArrayList class
  • Finally, we only need to print them, through a ForEach cycle, which basically returns each of the ArrayList objects and prints it.

It's a simple solution, you can do it without ArrayList, but I hope it helps, greetings.

SOLUTION WITHOUT ARRAYS

    String pares = "";
    String impares = "";
    int li, num;
    System.out.println("Ingrese la cantidad de nuemros a evaluar: ");
    Scanner sc = new Scanner(System.in);
    li = sc.nextInt();       

    for (int i = 1; i <= li; i++) {
        System.out.println("Ingrese su numero #" + i);
        num = sc.nextInt();
        if (num % 2 == 0) {
            pares += num + " ";
        } else {
            impares += num + " ";
        }
    }
    System.out.println("Los numeros pares son: " + pares);

    System.out.println("\nLos numeros inpares pares son: " + impares);

This is a simpler way, just concatenate the numbers to different chains with the operator + =, which is the same as saying cadena = cadena + "..." and then the result of each one is printed.

    
answered by 18.10.2018 / 05:37
source
0

Your problem is that you are using a single variable to store the different numbers and when you enter another you will be replaced, use an array that the way to declare them would be the following:

    int[] num;
    num = new int[li] //donde li es la cantidad de números a evaluar

Remember that when handling arrays you should always start counting from 0 ie num [0] would be the first value entered, the arrays can be traversed with a for

for(int i = 0, i < li, i++){
    System.out.println("Ingrese su numero #" + i + 1); // para mostrar desde 1
    num[i] = sc.nextInt();
}
if(num[i]%2==0)
    {
        np = num[i]; /*tambien diria que hagas de np un array para guardar diferentes valores */
    }

that would be a way to do it

    
answered by 18.10.2018 в 05:47