思路:首先对数组降序排序,然后取其第一个和第二个元素就可以实现。
代码如下:
public class BigNumber {
public static void main(String[] args) {
// 定义数组
int[] arrayInt = { 7, 2, 6, 5, 3, 4 };
// 排序
for (int i = 0; i < arrayInt.length - 1; i++) {
for (int j = 0; j < arrayInt.length - i - 1; j++) {
if (arrayInt[j] < arrayInt[j + 1]) {
int temp = arrayInt[j];
arrayInt[j] = arrayInt[j + 1];
arrayInt[j + 1] = temp;
}
}
}
// 最大值
int maxNum = arrayInt[0];
// 次大值
int secondNum = arrayInt[1];
System.out.println(maxNum + "," + secondNum);
}
}
运行结果: