日期:2014-05-20 浏览次数:21172 次
public class QuestionOne {
public static int[] temp = new int[10];
public static int[] result = new int[10];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Random rand = new Random();
// 循环为第一个数组赋值
for (int i = 0; i < temp.length; i++) {
System.out.println("请输入第" + i + "元素的值");
temp[i] = sc.nextInt();
}
System.out.print("排序前:");
// 循环输出第一个数组
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "\t");
}
System.out.println();
System.out.print("排序后:");
// 外城循环控制输出的个数
for (int i = 0; i < result.length; i++) {
// 产生一个长度为temp.length的随机数
int number = rand.nextInt(temp.length);
//若和以前的随机数重复则重新生成随机数直到新的不重复数字出现
while (temp[number]==0) number = rand.nextInt(temp.length);
// 为result数组随机的第几个元素赋值
result[i] = temp[number];
temp[number] = 0; //做记号,表示此序号已经处理过,以后不能再使用此序号
System.out.print(result[i] + "\t");
}
}
}