将数字1-10保存到一个长度为10的一维数组中
定义一个新数组,长度为3,取出原来数组中随机三个元素(不考虑是否重复)
给新数组的元素赋值
求新数组所有元素的和
import java.util.Random;
public class test1 {
public static void main(String[] args) {
Random r=new Random();
int[]arr1={1,2,3,4,5,6,7,8,9,10};
int[]arr2=new int[3];
System.out.println(“新数组的元素为:”);
for (int i = 0; i < arr2.length; i++) {
arr2[i]=arr1[r.nextInt(10)];
System.out.print(arr2[i]+" ");
}
System.out.println();
int num=0;
for (int i = 0; i < arr2.length; i++) {
num+=arr2[i];
}
System.out.println(“新数组的元素和为:”);
System.out.println(num);
}
}