importjava.util.Arrays;importjava.util.Random;publicclassWork02{publicstaticvoidmain(String[] args){Random ran =newRandom();int[] list =newint[6];int sum =0;for(int i =0;i < list.length; i++){list[i]= ran.nextInt(100);sum += list[i];}System.out.println(sum);System.out.println(Arrays.toString(list));}}
3. 需求:求出数组中索引与索引对应的元素都是奇数的元素
importjava.util.Arrays;importjava.util.Random;publicclassWork03{publicstaticvoidmain(String[] args){Random ran =newRandom();int[] arr =newint[8];for(int i =0; i < arr.length; i++){arr[i]= ran.nextInt(100);if(i %2!=0& arr[i]%2!=0){System.out.println(arr[i]);}}System.out.println(Arrays.toString(arr));}}
4. 按要求在main方法中完成以下功能:
a. 定义一个长度为5的int型数组arr,提示用户输入5个1-60之间的数字作为数组元素
b. 生成2-10(范围包含2和10)之间的随机数num
c. 遍历数组arr,筛选出数组中不是num倍数的元素并输
PS:输入的数组元素范围包括1和60,不需要代码判断
importjava.util.Random;importjava.util.Scanner;publicclassWork04{publicstaticvoidmain(String[] args){int[] arr =newint[5];Random ran =newRandom();int num = ran.nextInt(9)+2;Scanner input =newScanner(System.in);for(int i =0; i <5; i++){System.out.printf("请输入第%d个数据(1~60)\n",i+1);arr[i]= input.nextInt();}for(int i: arr){if(i % num !=0){System.out.println(i);}}}}
importjava.util.Random;importjava.util.Scanner;publicclassWork06{publicstaticvoidmain(String[] args){Scanner input =newScanner(System.in);Random ran =newRandom();int sum =0;System.out.println("请输入一个20~40之内的值");int num = input.nextInt();int[] arr =newint[20];for(int i =0; i < arr.length; i++){arr[i]= ran.nextInt(20)+20;if(arr[i]== num){sum +=1;}}System.out.println(sum);}}
importjava.util.Arrays;importjava.util.Random;publicclassWork09{publicstaticvoidmain(String[] args){Random ran =newRandom();int[] arr =newint[6];for(int i =0; i < arr.length; i++){arr[i]= ran.nextInt(101);}int sum =0;for(int i:arr){if(i !=Arrays.stream(arr).max().getAsInt()& i !=Arrays.stream(arr).min().getAsInt()){sum += i;}}double avg =(double) sum /4;System.out.println(avg);}}
10. 现有一个int 数组,数组中有十个元素。将数组反转后输出。
importjava.util.Arrays;publicclassWork10{publicstaticvoidmain(String[] args){int[] arr ={0,1,2,3,4,5,6,7,8,9};for(int i =0; i < arr.length /2; i++){arr[i]= arr[i]^ arr[arr.length-i-1];arr[arr.length-i-1]= arr[i]^ arr[arr.length-i-1];arr[i]= arr[i]^ arr[arr.length-i-1];}System.out.println(Arrays.toString(arr));}}