给出一组年龄,用冒泡排序求最大年龄,最小年龄 发表于 2017-12-01 | 更新于 2018-08-15 | 分类于 Java | 评论数: | 阅读次数: 法一123456789101112131415161718//求出最大年龄,求出最小年龄,求出平均年龄 int[] ages = {34,11,45,23,16}; boolean flag = true ; for (int i = 4; flag && i >0 ; i--) { flag = false ; for (int j = 0; j < i; j++) { if(ages[j]>ages[j+1]){ int temp = ages[j]; ages[j] = ages[j+1]; ages[j+1] = temp ; flag = true ; } } System.out.println("-------"); } for (int i = 0; i < 5; i++) { System.out.print(ages[i]+ "\t"); } 法二12345678910111213141516171819int[] ages = {1,2,3,4,5}; boolean flag = true ; for (int i = 0; flag && i < ages.length-1; i++) { flag = false ; for (int j = 0; j < ages.length - i - 1; j++) { if(ages[j] > ages[j+1]){ int temp = ages[j]; ages[j] = ages[j+1]; ages[j+1] = temp ; flag = true ; } } System.out.println("-----"); } for (int i = 0; i < ages.length; i++) { System.out.print(ages[i]+"\t"); }