在一个方法中实现从一个数组中找到该数组的最大值和次大值并返回。
import java.util.Scanner;public class four { void getTwo(int[] a) { for (int i = 0; i int max = i; for (int j = i + 1; j if (a[max] int temp = a[max]; a[max] = a[j]; a[j] = temp; } } } } public static void main(String[] args) { Scanner reader = new Scanner(System.in); int[] arr = { 2, 43, 65, 667, 342, 5645, 463, 34 }; four ar = new four(); ar.getTwo(arr); System.out.println("最大值:" + arr[0] + "次大值:" + arr[1]); }}
运行结果: