-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayExample.java
More file actions
36 lines (30 loc) · 1.23 KB
/
ArrayExample.java
File metadata and controls
36 lines (30 loc) · 1.23 KB
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
36
import java.util.Random;
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = new int[100];
Random random = new Random();
for (int i = 0; i < numbers.length; i++) {
numbers[i] = random.nextInt(100);
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter an index to display the array value (or type 'exit' to quit):");
String userInput = scanner.next();
if (userInput.equalsIgnoreCase("exit")) {
break;
}
try {
int index = Integer.parseInt(userInput);
System.out.println("Element value at index " + index + ": " + numbers[index]);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds. Please enter a number between 0 and 99.");
}
}
// Close the scanner
scanner.close();
System.out.println("Program terminated.");
}
}