public class find第二大数 {
public static void main(String[] args) {
int[] a = new int[]{1,2,5,12,45,5454,5,5,5,55,54,54,21,1121,212,54504};
findSecondMax(a);
}
private static void findSecondMax(int[] a) {
int firstMax = Math.max(a[0],a[1]);
int secondtMax = Math.min(a[0],a[1]);
for(int i = 2;ifirstMax){
secondtMax = firstMax;
firstMax = a[i];
}
else if(a[i]>secondtMax){
secondtMax = a[i];
}
}
if(firstMax==secondtMax){
System.out.println("无");
}else{
System.out.println(secondtMax);
}
}
}
一个数组中寻找第二大数字
最新推荐文章于 2019-11-14 23:50:23 发布