Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Brandon Edmonson
- * CSC 275 - B
- * Lab #1
- * Create a program that has an array of length 100 which will
- * automatically be filled with randomly generated numbers between
- * 1 and 100 each time the program is run. Have the program ask
- * the user to enter a number between 1 and 100. Check to see if
- * that number is one of the values in the array. If it is, display
- * "We found your number XX at index YY in the array". If the number
- * is not in the array, tell the user that no match was found.
- */
- import java.util.Scanner;
- public class Lab01
- {
- public static void main(String[] args)
- {
- int[] randomArray;
- randomArray = new int[100];
- for (int i = 0; i < randomArray.length; i++)
- {
- randomArray[i] = (int) (Math.random() * 101);
- }
- for (int i = 0; i < randomArray.length; i++)
- {
- System.out.println("Index " + i + " is: " + randomArray[i]);
- }
- Scanner input = new Scanner(System.in);
- int number;
- System.out.println("Please enter a number to search for: ");
- number = input.nextInt();
- boolean found = false;
- for (int i = 0; i <randomArray.length; i++)
- {
- if (number == randomArray[i])
- {
- found = true;
- }
- if (found)
- {
- System.out.println("We found your number " + number +
- " at index: " + i);
- }
- else
- {
- System.out.println("We did not find your number");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement