Directory
1 Problem Description
2 Solutions
1 problem description Problem DescriptionWrite a program that reads in a set of integers (no more than 20), and when the user enters 0 o'clock, the input ends. The program will then find the second largest integer from this set of integers and print it out. Note: (1) 0 indicates the end of the input, which itself is not counted in this set of integers. (2) In this set of integers, both positive and negative numbers are possible. (3) The number of integers in this group is not less than 2.
input Format: Enter only one line, including several integers, separated by a space, the last integer is 0.
output format: Output the second largest integer.
input/Output sampleSample Input5 8-12 7 0Sample Output7
2 Solutions
The specific code is as follows:
Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {ArrayList<Integer> list =NewArraylist<integer>(); Scanner in=NewScanner (system.in); while(true) { intA =In.nextint (); if(A = = 0) Break; List.add (a); } collections.sort (list); System.out.println (List.get (list.size ()-2)); }}
Algorithm Note _159: algorithm to increase the second largest integer (Java)