Home > Enterprise >  array indentifier range whether it is yes or no within that range int JAVA
array indentifier range whether it is yes or no within that range int JAVA

Time:11-07

I am newbie in java program and I am currently working with this problem because I can't beat it.

My Source Code:

import java.util.*;
public class Main {
   public static void main(String[] args)
   {
     Scanner x = new Scanner (System.in);
     System.out.print("Enter the size of the array: ");
     int elements = x.nextInt();

     int a[]= new int[elements];
     for(int y=0;y<a.length;y  )
     {
        System.out.print("Element #" (y 1) ": ");
        a[y]=x.nextInt();
     }   
   }      
}

Output:

Enter the size of the array: 2
Element #1: 2
Element #2: 3

Possible Output should flash like this:

Enter the size of the array: 4
Element #1: 2421
Element #2: 3245
Element #3: 1324
Element #4: 2443
Enter x and y: 2000 3000

Element #1 = YES
Element #2 = NO
Element #3 = NO
Element #4 = YES

CodePudding user response:

import java.util.*;
public class Main {
   public static void main(String[] args)
   {
     Scanner x = new Scanner (System.in);
     System.out.print("Enter the size of the array: ");
     int elements = x.nextInt();

     int a[]= new int[elements];
     for(int y=0;y<a.length;y  )
     {
        System.out.print("Element #" (y 1) ": ");
        a[y]=x.nextInt();
     }
     System.out.print("Enter x and y: ");
     int lb = x.nextInt();
     int ub = x.nextInt();
     for(int y = 0; y < a.length; y  ) {
         if(a[y] >= lb && a[y] <= ub) {
             System.out.println("Element #"   (y   1)   " = YES");
         }
         else {
             System.out.println("Element #"   (y   1)   " = NO");
         }
     }
   }      
}
  •  Tags:  
  • java
  • Related