Exercise: Array With Random Numbers
Write a program that
- creates an integer array with 10 numbers,
- fills the array with random numbers between 1 and 10 (inclusive),
- prints out all numbers exactly as shown below (no trailing comma),
- prints out the sum of the numbers,
- and prints out the largest number.
For example:
The array: 5, 6, 5, 10, 3, 7, 6, 8, 3, 2
Sum of all numbers: 55
The largest number is: 10
Hint 💡
When creating random numbers, you only need to initialize the
random
object once. You can then use the same object to generate random numbers in a loop:
Random random = new Random();
int lowerInclusive = 0;
int upperInclusive = 10;
for (...) {
int randNr = random.nextInt(lowerInclusive, upperInclusive + 1);
}
Hand in instructions
- Make sure your program runs correctly.
- Hand in your program by uploading Main.java to Moodle.