Blonk

Untitled

Feb 26th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.*;
  3. import java.util.concurrent.ThreadLocalRandom;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Random rand = new Random();
  11.         Scanner keyboard = new Scanner(System.in);
  12.  
  13.         int[] myList;
  14.         myList = new int[10];
  15.         int slot = 0;
  16.         boolean foundInt = false;
  17.  
  18.         System.out.print("Array: ");
  19.         for (int i = 0; i < myList.length; i++) {
  20.             myList[i] = 1 + rand.nextInt(50);
  21.             System.out.print(myList[i] + " ");
  22.         }
  23.  
  24.         System.out.println();
  25.         System.out.println("\nValue to find: ");
  26.         int arrayChosen = keyboard.nextInt();
  27.  
  28.         for (int i = 0; i < myList.length; i++) {
  29.             if (myList[i] == arrayChosen && !foundInt) {
  30.                 slot = i;
  31.                 foundInt = true;
  32.             }
  33.         } System.out.println();
  34.  
  35.         if (foundInt){
  36.             System.out.println(arrayChosen + " is in slot " + slot);
  37.         } else {
  38.             System.out.println(arrayChosen + " is not in the array");
  39.         }
  40.     }
  41. }
Add Comment
Please, Sign In to add comment