赞
踩
方法一:直接使用Arrays里的sort方法
- import java.util.*;
-
- public class App {
-
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- char[]chars = str.toCharArray();
- Arrays.sort(chars);
- System.out.println(new String(chars));
- }
- }
方法二:使用双重循环
- import java.util.*;
-
- public class App {
-
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- char[]chars = str.toCharArray();
- char temp;
- for (int i=0;i<chars.length;i++){
- for (int j=0;j<chars.length-1;j++){
- if(chars[j]>chars[j+1]){
- temp = chars[j];
- chars[j] = chars[j+1];
- chars[j+1] = temp;
- }
- }
- }
- String printStr = "";
- for (int k=0;k<chars.length;k++){
- printStr = printStr + chars[k];
- }
- System.out.println(printStr);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。