java 字符串排序

输入一个字符串,排序后输出~
例:输入:abACc 输出:ACabc
就是按ask码输出~
谢谢~
很急~

public class Test1{
public static void main(String[] args)
{
char[] str=new char[1024];
char temp=0;
int ch=0;
int pos=0;
while(true)
{

try
{
ch=System.in.read();
}
catch(Exception e)
{
e.printStackTrace();
}
if(ch=='\r' || ch=='\n')
{
break;
}
else
{
str[pos++]=(char)ch;

}

}
for(int x=0;x<pos;x++)
{
for(int y=0;y<pos-x;y++)
{
if(str[y]>str[y+1])
{
temp=str[y+1];
str[y+1]=str[y];
str[y]=temp;
}

}
}
System.out.println(new String(str,0,pos+1));
}
}
我也是新手 弄了半天 唉
惭愧
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-08-25
来个简单点的,JDK自带的方法

import java.util.Arrays;
public class Test001 {

public static void main(String[] args) {
System.out.println(sort("abACc"));
}

/** 排序 */
public static String sort(String inString){
char[] chars = inString.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
}
第2个回答  2013-08-25
import java.util.Queue;
import java.util.*;
import java.util.Scanner;

public class M {
public static void main(String[] args)
{
String x=new String();
Scanner scanner = new Scanner(System.in);
try {
x = scanner.next();
}catch(java.util.InputMismatchException ime) {
//if the next token does not match the Integer regular
//expression, or is out of range
System.out.println("not a integer");
}

int[] s=new int[x.length()];
for(int i=0;i<x.length();i++)
s[i]=x.charAt(i);
//用冒泡排序法对al数组进行排序,从小到大
for(int i=0;i<10;i++)
for(int j=i+1;j<10;j++)
{
int temp;
if(s[j]<s[i])
{
temp=s[j];s[j]=s[i];s[i]=temp;
}
}

for(int i=0;i<x.length();i++)
{
System.out.print((char) s[i]);
}
}
}
第3个回答  2013-08-25
public class sort(){
public static void main(String args[]){
String s="12345678";
char[] ch = s.toCharArray(s);
StringBuffer sb = new StringBuffer();
for(int i=ch.length()-1;i>=0;i++){
sb.append(ch[i]);
}
}
}
第4个回答  2013-08-25
很简单,字符数组有个sort方法,参数是数组本身,咻的一下就排好了
相似回答