-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnterArrayAndDisplay.java
More file actions
37 lines (24 loc) · 1.01 KB
/
EnterArrayAndDisplay.java
File metadata and controls
37 lines (24 loc) · 1.01 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
37
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class EnterArrayAndDisplay {
public static void main(String[] args) {
// Create an array to store 10 values
int[] array = new int[10];
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 10 values:");
// Use a loop to get input for each element in the array
for (int i = 0; i < 10; i++) {
System.out.print("Enter value for element " + i + ": ");
// Read the input and store it in the array
array[i] = scanner.nextInt();
}
// Close the Scanner to avoid resource leak
scanner.close();
// Display the entered values
System.out.println("Entered values in the array:");
for (int i = 0; i < 10; i++) {
System.out.println("Element " + i + ": " + array[i]);
}
}
}