Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2. /**
  3. * Brandon Edmonson
  4. * CSC 275 - B
  5. * Lab #1
  6. * Create a program that has an array of length 100 which will
  7. * automatically be filled with randomly generated numbers between
  8. * 1 and 100 each time the program is run. Have the program ask
  9. * the user to enter a number between 1 and 100. Check to see if
  10. * that number is one of the values in the array. If it is, display
  11. * "We found your number XX at index YY in the array". If the number
  12. * is not in the array, tell the user that no match was found.
  13. */
  14. import java.util.Scanner;
  15.  
  16. public class Lab01
  17. {
  18. public static void main(String[] args)
  19. {
  20. int[] randomArray;
  21. randomArray = new int[100];
  22.  
  23. for (int i = 0; i < randomArray.length; i++)
  24. {
  25. randomArray[i] = (int) (Math.random() * 101);
  26. }
  27.  
  28. for (int i = 0; i < randomArray.length; i++)
  29. {
  30. System.out.println("Index " + i + " is: " + randomArray[i]);
  31. }
  32. Scanner input = new Scanner(System.in);
  33. int number;
  34. System.out.println("Please enter a number to search for: ");
  35. number = input.nextInt();
  36.  
  37. boolean found = false;
  38. for (int i = 0; i <randomArray.length; i++)
  39. {
  40. if (number == randomArray[i])
  41. {
  42. found = true;
  43. }
  44.  
  45. if (found)
  46. {
  47. System.out.println("We found your number " + number +
  48. " at index: " + i);
  49. }
  50. else
  51. {
  52. System.out.println("We did not find your number");
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement