输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
int f=input.nextInt();
while(f>0){
String str;
Scanner sc = new Scanner(System.in);
//System.out.print("请输入字符串:");
str = sc.nextLine();
char[] chs = str.toCharArray();
bubbleSort(chs);
//System.out.println(bubbleSort(chs));
--f;
}
}
    public static void bubbleSort(char[] chs)
    {
        char temp ='a';
        int size = chs.length;
        for(int i = 0 ; i < size-1; i ++)
        {
        for(int j = 0 ;j < size-1-i ; j++)
        {
            if(chs[j] > chs[j+1])  //交换两数位置
            {
            temp = chs[j];
            chs[j] = chs[j+1];
            chs[j+1] = temp;
            }
        }
        }
        for(int j = 0 ;j < size ; j++)
        {
            System.out.print(chs[j]);
            System.out.print(' ');
        }
        //return chs;
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_38291260/article/details/74506112