int[] arr = new int[10];
boolean shuchu = false;
//随机生成1-100之间的10个数保存到数组
for (int i = 0; i < 10; i++) {
int a = (int) (Math.random() * 100);
arr[i] = a;
System.out.print(arr[i]+" ");
if (arr[i] == 8){
shuchu = true;
}
}
if (shuchu) {
System.out.println();
System.out.print("数组中含有8");
}
//将数组进行正向排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j + 1] < arr[j]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println();
System.out.print(Arrays.toString(arr));//正常打印
System.out.println();
System.out.println("数组最大值="+arr[arr.length-1]);
//将数组倒序排列
int[] arr1 = new int[arr.length];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = arr[arr.length - i - 1];
}
System.out.print(Arrays.toString(arr1));
System.out.println();
System.out.println("数组最小值="+arr1[arr.length-1]);
//求出数组平均值
int sum = 0;
for (int i = 0; i < arr1.length; i++) {
sum+=arr1[i];
}
System.out.println("数组平均值="+sum/ arr1.length);
Java练习(十八)---随机生成1-100中的10个数&倒序排列&求平均值&输出最大值最小值&判断是否有8
于 2022-02-27 00:13:36 首次发布