如题所述
int[] arr = new int[10];
int temp = 0;
int key = 0;
arr[0] = (int) (Math.random() * 100);
arr[9] = (int) (Math.random() * 100);
if (arr[9] < arr[0]) {
key = arr[9];
arr[9] = arr[0];
arr[0] = key;
}
for (int i = 1; i < 9; i++) {
temp = (int) (Math.random() * 100);
if (temp < arr[0]) {
key = arr[0];
arr[0] = temp;
arr[i] = key;
} else if (temp > arr[9]) {
key = arr[9];
arr[9] = temp;
arr[i] = key;
} else {
arr[i] = temp;
}
}追问
int temp = 0;
int key = 0;
arr[0] = (int) (Math.random() * 100);
arr[9] = (int) (Math.random() * 100);
if (arr[9] < arr[0]) {
key = arr[9];
arr[9] = arr[0];
arr[0] = key;
}
for (int i = 1; i < 9; i++) {
temp = (int) (Math.random() * 100);
if (temp < arr[0]) {
key = arr[0];
arr[0] = temp;
arr[i] = key;
} else if (temp > arr[9]) {
key = arr[9];
arr[9] = temp;
arr[i] = key;
} else {
arr[i] = temp;
}
}追问
舒服的结果是最大的在最后面,最小的不再第一个
追答在第一个啊 我试过了
你出现的什么结果
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-05-14
楼上的可以实现最小数放在第一个元素的位置,将最大数放在最后一个位置。只是不是严格的递增而已