forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise12_03.java
35 lines (25 loc) · 1.01 KB
/
Exercise12_03.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ch_12;
import java.util.Scanner;
/**
* 12.3 (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements:
* Creates an array with 100 randomly chosen integers
* Prompts the user to enter the index of the array, then displays the corresponding element value.
* If the specified index is out of bounds, display the message Out of Bounds.
*/
public class Exercise12_03 {
public static void main(String[] args) {
int[] randomArray = new int[100];
for (int i = 0; i < randomArray.length; i++) {
randomArray[i] = (int) (1 + Math.random() * 10_000);
}
Scanner input = new Scanner(System.in);
System.out.println("Enter an index in the array for which to display it's value: ");
try {
int indexValue = input.nextInt();
System.out.println(randomArray[indexValue]);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Out Of Bounds");
}
input.close();
}
}