package hwtest.total;
public class ArrayMaxTwoNum {
public static void main(String[] args) {
int[] array = { 1, 2, 5, 9, 84, 3, 2 };
System.out.println("数组" + array.toString() + "里面最大的2个数为:");
findMaxTwoNum(array);
}
public static void findMaxTwoNum(int[] array) {
if (array.length < 2)
return;
int[] result = { array[0], array[1] };
for (int i = 2; i < array.length; i++) {
if (array[i] > result[0] || array[i] > result[1]) {
if (result[0] < result[1])
{
result[0] = array[i];
} else {
result[1] = array[i];
}
}
}
System.out.println("数组" + array.toString() + "里面最大的2个数为:"+result[0]+","+result[1]);
}
}
public class ArrayMaxTwoNum {
public static void main(String[] args) {
int[] array = { 1, 2, 5, 9, 84, 3, 2 };
System.out.println("数组" + array.toString() + "里面最大的2个数为:");
findMaxTwoNum(array);
}
public static void findMaxTwoNum(int[] array) {
if (array.length < 2)
return;
int[] result = { array[0], array[1] };
for (int i = 2; i < array.length; i++) {
if (array[i] > result[0] || array[i] > result[1]) {
if (result[0] < result[1])
{
result[0] = array[i];
} else {
result[1] = array[i];
}
}
}
System.out.println("数组" + array.toString() + "里面最大的2个数为:"+result[0]+","+result[1]);
}
}