import java.util.Scanner;
class Demo1 {
public static void main(String[] args) {
int [] arr = new int [5] ;
Scanner sc = new Scanner(System.in);
System.out.println("存入第一个数据:");
arr[0] = sc.nextInt();
System.out.println("存入第二个数据:");
arr[1] = sc.nextInt();
System.out.println("存入第三个数据:");
arr[2] = sc.nextInt();
System.out.println("存入第四个数据:");
arr[3] = sc.nextInt();
System.out.println("存入第五个数据:");
arr[4] = sc.nextInt();
print(arr);
int max = maxArray(arr);
System.out.println("最大值");
System.out.println(max);
System.out.println("排序后:");
sortArray(arr);
print(arr);
}
public static void print(int [] arr) {
for (int x = 0;x <arr.length ;x++ ) {
if (x!=arr.length-1) {
System.out.print(arr[x]+" ");
} else {
System.out.println(arr[x]);
}
}
}
public static int maxArray(int[] arr) {
int max = arr[0];
for (int x = 1;x < arr.length;x++) {
if (max<arr[x]) {
int temp = max;
max= arr[x];
arr[x]=temp;
}
}
//return arr[max];
return max;
}
public static void sortArray(int [] arr) {
for (int x = 0;x <arr.length ;x++ ) {
for (int y = 0;y <arr.length-1-x ;y++ ) {
if (arr[y]>arr[y+1]) {
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1]=temp;
}
}
}
}
}
|
|