如输入 bdca 输出 abcd
第一种算法:
public class Test03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串字符串:");
String str = sc.nextLine();
char[] a = str.toCharArray();
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
}
}
第二种算法:
public class Test03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串字符串:");
String str = sc.nextLine();
char[] a = str.toCharArray();
Arrays.sort(a);
System.out.println(new String(a));
}
}