Home > Net >  Output all the odd and even numbers in the random numbers array
Output all the odd and even numbers in the random numbers array

Time:01-07

I declare an integer array of size 300, with random values from 1 to 500. I need to output all the even and odd numbers in the array 10 per line.

this is my code but I can't print 10 numbers per line and it doesn't print enough even and odd numbers in my array:

import java.util.Scanner;

public class Array {

   public static void main(String[] args)
   { 
      Scanner keyboard=new Scanner(System.in); 
   
      
      int[] A = new int[300]; 
      for(int i=0;i<A.length;i  )
      {
         A[i]=(int)(Math.random()*500 1);
      }
      
      for(int i=0;i<A.length;i  ){
         System.out.print(A[i] " ");
         if(((i 1) % 10) == 0){
            System.out.println();
         }         
      }
      System.out.println("Odd Numbers:"); 
       
      for(int i=0;i<A.length;i  ){  
         if(A[i]%2!=0 && ((i 1) % 10) == 0){
            System.out.print(A[i] " ");
         }
      }
      System.out.println();
      System.out.println("Even Numbers:"); 
      for(int i=0;i<A.length;i  ){
      
         if(A[i]%2==0 && ((i 1) % 10) == 0){  
            System.out.print(A[i] " ");  
         }  
      }
     
   
      
   }
}

CodePudding user response:

You have to use a counter and check if it count % 10 == 0. If yes, then move to the next line.

public static void main(String... args) {
    final IntPredicate odd = value -> value % 2 != 0;
    final IntPredicate even = value -> value % 2 == 0;

    int[] arr = randomArray(300);

    System.out.println("Odd Numbers:");
    printNumbers(arr, odd);

    System.out.println();
    System.out.println("Even Numbers:");
    printNumbers(arr, even);
}

private static int[] randomArray(int total) {
    Random random = new Random();

    return IntStream.range(0, total)
                    .map(i -> random.nextInt(500)   1)
                    .toArray();
}

private static void printNumbers(int[] arr, IntPredicate predicate) {
    int i = 0;

    for (int value : arr) {
        if (predicate.test(value)) {
            System.out.format("= ", value);

            if (  i % 10 == 0)
                System.out.println();
        }
    }

    System.out.println();
}

CodePudding user response:

It looks like you are trying to print 10 numbers per line, but you are using the wrong conditional statement to check if the current number is the last number in the line.

In your code, you are using the following conditional statement:

if(((i 1) % 10) == 0)

This statement is checking if the current number is the 10th number in the line, but you want to check if it is the last number in the line.

To fix this, you can use the following conditional statement instead:

if((i 1) % 10 == 0)

This will check if the current number is the last number in the line, and will print a newline character if it is.

You will also need to make sure that you are using the correct loop variable to check if the current number is even or odd. Right now, you are using the same loop variable i for all three loops, which means that you are only printing a small subset of the even and odd numbers in the array.

To fix this, you can use separate loop variables for each loop. For example:

for(int i=0;i<A.length;i  ){  
    if(A[i]%2!=0){
        System.out.print(A[i] " ");
    }
    if((i 1) % 10 == 0){
        System.out.println();
    }
}

System.out.println("Even Numbers:"); 
for(int j=0;j<A.length;j  ){  
    if(A[j]%2==0){  
        System.out.print(A[j] " ");  
    }  
    if((j 1) % 10 == 0){
        System.out.println();
    }
}

This will print all the odd numbers in the array, followed by all the even numbers in the array, with 10 numbers per line.

  • Related