在控制台任意输入一组数字进行排序
小弟的JAVA基础不好,请各位指点~

解决方案 »

  1.   

    我也是新手,前几天写的快速排序加以修改,楼主看满足你的要求不?
    import java.io.*;
    public class QuickSort {
    public static void sort(int[] a){
    sort(a,0,a.length-1);
    }
    public static void sort(int[] a ,int i ,int j){
    if(i < j){
    int low = i ,high = j ,temp = a[i];
    while(low != high){
    while(low < high && a[high]>temp)high--;
    if(low < high)a[low++]=a[high];
    while(low < high && a[low]<temp)low++;
    if(low < high)a[high--]=a[low];
    }
    a[low]=temp;
    sort(a,i,low-1);
    sort(a,low+1,j);
    }
    }
    public static void print(int[] a){
        for(int v:a){
         System.out.print(v + " ");
        }
    }
    public static void inputNum() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
    System.out.println("输入你要排序的数,中间用空格分开");
    String  s = br.readLine().trim();
    String [] stringNums = s.split("\\s+");
                               int[] num = typeOf(stringNums);
                               sort(num);
                               print(num);
    }catch(NumberFormatException e1){
    throw new RuntimeException("输入的类型不正确");
        }
    catch(IOException e2){
    e2.printStackTrace();
    }
    }
    public static int[] typeOf(String[] s){
    int[] number = new int[s.length];
    int i = 0;
    for(String str:s){
    number[i++] = Integer.parseInt(str);
    }
    return number;
    }
    public static void main(String[] args) {
                      inputNum();
    }
    }
      

  2.   

    import java.io.*;
    public class pai
    {
    public static void main(String args[]) throws Exception 
    {BufferedReader put=new BufferedReader(new InputStreamReader(System.in));
    String input = put.readLine();int length=input.length();
    char sub[]=new char[length];
    input.getChars(0,length,sub,0);
    for(int i=0;i<length-1;i++)
    {
    if(sub[i]>sub[i+1])
    {
    sub[i+1]=sub[i];
    }
    }System.out.print(sub); }
    }