import java.util.Random;
public class EvenNumber {
public static void main(String[] args) {
//定义一个长度为20的int类型数组
int [] arr = new int [20];
Random rd = new Random();
//100-999随机数
for (int i = 0; i < 20; ) {
int num = rd.nextInt(900)+100;
//如果是偶数就加到数组中,并且i+1
if(num % 2 == 0){
arr [i++] = num ;
}
}
System.out.println("100-999的20个随机偶数为");
//定义一个和的变量
int count = 0 ;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
count += arr [i];
}
System.out.println("20个随机偶数和为:"+count);
}
}
|