Java编写程序,产生10个随机数,然后按产生顺序相反的次序输出这些随机数。急!

2024-11-22 19:48:56
推荐回答(1个)
回答1:

import java.util.Random;

public class Demo {
public static void main (String args[]) {
//定义一个数组存放随机数:
int[] randoms = new int[10];

//产生10个随机数:
Random rnd = new Random();
for (int i=0; i randoms[i] = rnd.nextInt();

//正序输出:
System.out.println("正序输出:");
for (int i=0; i System.out.println(randoms[i]);

//逆序输出:
System.out.println("逆序输出:");
for (int i=randoms.length-1; i>=0; i--)
System.out.println(randoms[i]);
}
}