任何人都知道我怎么能限制这个程序只打印数组的20个元素中的5个。
问候和感谢。
import java.util.*;
public class lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < n.length; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
答案 0 :(得分:1)
import java.util.*;
public class Lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
了解如何使用for循环http://en.wikipedia.org/wiki/For_loop#Java