作者: guiliujun 时间: 2016-7-3 17:26
public static void main(String[] args) {
int [] arr = {6,2,9,15,1,5,20,7,18};
int min = arr[0];
int max = arr[0];
for (int i = 0;i < arr.length ;i++ ) {
if (max<arr[i]) {
max=arr[i];
}
if (min>arr[i]) {
min=arr[i];
}
}
for (int i = 0; i < arr.length; i++ ) {
if (arr[i] == max) {
int temp1 = arr[i];
arr[i] = arr[arr.length - 1];
arr[arr.length - 1] = temp1;
}
if (arr[i] == min) {
int temp2 = arr[i];
arr[i] = arr[0];
arr[0] = temp2;
}
}
for (int i = 0; i < arr.length; i++ ){
System.out.print(" " + arr[i]);
}
}
}作者: 新人—小明同学 时间: 2016-7-4 01:22
class Test {
public static void main(String[] args) {
int[] arr = new int[]{6,2,9,15,1,5,20,7,18};
print(arr);
System.out.println();
int maxMark = getMaxMark(arr);
System.out.println(maxMark);
int minMark = getMinMark(arr);
System.out.println(minMark);
revArray(arr,maxMark);
rArray(arr,minMark);
print(arr);
}
public static void print(int[] arr) {
for (int i = 0;i < arr.length ;i++ ) {
System.out.print(arr[i] + ",");
}
}
public static int getMaxMark(int[] arr) {
int mark = arr[0];
for (int i = 0;i < arr.length ;i++ ) {
if (arr[mark] < arr[i]) {
mark = i;
}
}
return mark;
}
public static int getMinMark(int[] arr) {
int min = arr[0];
for (int i = 0;i < arr.length ;i++ ) {
if (arr[min] > arr[i]) {
min = i;
}
}
return min;
}
public static void revArray(int[] arr,int maxMark) {
int temp = 0;
temp = arr[arr.length - 1];
arr[arr.length - 1] = arr[maxMark];
arr[maxMark] = temp;
}
public static void rArray(int[] arr,int minMark) {