Advertisement
lol1234561

Untitled

Sep 29th, 2023
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Random rand =  new Random();
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         int[] num = new int[10];
  9.  
  10.         int total = 0;
  11.         boolean found = false;
  12.  
  13.         System.out.print("Array 1: ");
  14.  
  15.         for( int x = 0; x < num.length; x++){
  16.             num[x] = rand.nextInt(100)+1;
  17.             System.out.print( num[x] + " ");
  18.  
  19.         }
  20.         System.out.print("\nValue to find: ");
  21.         int value = scan.nextInt();
  22.  
  23.  
  24.  
  25.         for (int x: num){
  26.             if( x == value){
  27.                 total = total + 1;
  28.                 found = true;
  29.             }
  30.         }
  31.         if(found){
  32.             System.out.println(value + " is in the array.");
  33.         }
  34.         if(!found){
  35.             System.out.println(value + " is not in the array.");
  36.         }
  37.  
  38.         System.out.println(value + " was found a total of: " + total + " time(s)." );
  39.  
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement