2编写一个有关数组的程序(1)输出自己的信息(2)输出数组(3)通过循环语句求出最大值、最小值和总和,接着计算平均值,然后输出最大值、最小值和平均值(4)通过双重循环对数组排序(可用冒泡法排序),并输出排序后的数组(5)提交代码文件Array.java
2条回答 默认 最新
- ToJo斩美 2022-11-15 03:54关注
public class Array { public static void main(String[] args) { int[] a = {33,80,60,99,51,73}; System.out.println("原始数组:"); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; double pj=0; for (int i : a) { System.out.print(i+" "); max = Integer.max(max, i); min = Integer.min(min, i); pj+=i; } System.out.println(); System.out.println("最大值:"+max); System.out.println("最小值:"+min); System.out.println("平均值:"+pj/a.length); System.out.print("排序后的数组"); for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { int temp; if (a[j]>a[j+1]){ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } System.out.println(" "); for (int i : a){ System.out.print(i+"\t\t"); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报