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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!