数组的定义和使用示例:
int[] a = new int[10];
a[0] = 1;
•
•
1
、编写一个方法,用于随机生成
5
,
8
,
11
,
14
,
17
中的一个数。
•
•
2
、编写一个方法,用于随机生成
5
,
8
,
11
,
14
,
19
中的一个数。
•
•
3
、随机生成
10000
个
1-10
范围的整数,并统计每个数出现的次数。
import java.util.Random;
public class SWE {
public static void main(String[] args) {
// TODO Auto-generated method stub
int randomN = getRandomN();
System.out.println("随机生成的数是:"+randomN);
}
public static int getRandomN(){
int[]numbers = {5,8,11,14,17};
Random random = new Random();
int index = random.nextInt(numbers.length);
return numbers[index];
}
}
import java.util.Random;
public class SWE {
public static void main(String[] args) {
// TODO Auto-generated method stub
int randomN = getRandomN();
System.out.println("随机生成的数是:"+randomN);
}
public static int getRandomN(){
int[]numbers = {5,8,11,14,19};
Random random = new Random();
int index = random.nextInt(numbers.length);
return numbers[index];
}
}
import java.util.Random;
public class SWE {
public static void main(String[] args) {
// TODO Auto-generated method stub
getRandomN();
}
public static void getRandomN(){
int[]numbers =new int[10];
Random random = new Random();
for(int i =0;i<1e4;i++){
int index = random.nextInt(numbers.length);
numbers[index]++;
}
for(int i =0;i<10;i++){
System.out.println(i+1+"出现"+numbers[i]);
}
}
}