MrDoyle

Arrays) Finding a Value in an Array

Jan 25th, 2021
379
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Integer [] Array = new Integer[10];
  8.  
  9.         Random rand = new Random();
  10.  
  11.         for (int i = 0 ; i < Array.length ; i++) {
  12.             int rand_int1 = rand.nextInt(51);
  13.             Array[i] = rand_int1;
  14.         }
  15.  
  16.         System.out.print("Array: ");
  17.         for (int i = 0 ; i < Array.length ; i++) {
  18.             System.out.print(Array[i] + " ");
  19.         }
  20.  
  21.         System.out.print("\nValue to find: ");
  22.         Scanner keyboard = new Scanner(System.in);
  23.         int numFind = keyboard.nextInt();
  24.  
  25.         System.out.println();
  26.  
  27.         for (int i = 0 ; i < Array.length ; i++) {
  28.             if (Array[i] == numFind) {
  29.                 System.out.println(numFind + " is in the Array.");
  30.             }
  31.         }
  32.  
  33.     }
  34. }
RAW Paste Data