用java编程,,选择排序问题,菜单包括:升序排序,降序排序.输入n个整数,对其进行排序(n由键

import java.util.Scanner;

public class Test {
public static int cd(){
int i ;
System.out.println("*********************");
System.out.println("*******选择排序******");
System.out.println("***** 1   å‡åº*******");
System.out.println("***** 2   é™åº*******");
System.out.println("***** 0   é€€å‡º*******");
System.out.println("*********************");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的选择:");
i = sc.nextInt();
return i;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入n:");
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
System.out.print("输入第[ " + (i + 1) +" ]个数 : ");
a[i] = sc.nextInt();
}
int i ;
i = cd();
while(i != 0){
switch (i) {
case 1:
for (i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j]) {
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
System.out.println("升序排序后的结果 : ");
for(int temp : a){
System.out.print(temp + " ");
}
System.out.println();
break;
case 2:
for (i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] < a[j]) {
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
System.out.println("降序排序后的结果 : ");
for(int temp : a){
System.out.print(temp + " ");
}
System.out.println();
break;
case 0:
System.out.println("exit!");
break;
}
i = cd();
}
}
}

温馨提示:答案为网友推荐,仅供参考