Question
JAVA: Write a program that fills an array with 10 random integers in the range 1-100. Then, find the largest and second largest integer in
JAVA: Write a program that fills an array with 10 random integers in the range 1-100. Then, find the largest and second largest integer in an array.
Here is my code:
import java.util.*; public class Question4 { public static void main(String[] args) { int[] values = new int[10]; int maxValue = 0; Random rand = new Random(); for(int i = 0; i < 10; i++) { values[i] = rand.nextInt(100) +1; if(values[i]>values[maxValue]) { maxValue = i; } } System.out.println("The array of integers is: "); for(int i = 0; i < 10; i++) { System.out.println(values[i]+ " "); } System.out.println(); System.out.println("The largest integer " +values[maxValue]+ " is at location " +(maxValue+1)); } }
So far I have only been able to find the largest integer. How do I find the second largest?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started