恩  面试被考到了 
 就四键盘输入一串数据,然后排序,
排序不是问题,但是如何 键盘接受数据呢?

解决方案 »

  1.   

    System.in就可以啊, 还有很多种方法.
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a line:");
    System.out.print(stdin.readLine());
    这是一次读一行.可以读完再排序, 或者一个一个的读, 边读边排序都行.
      

  2.   

    这个不会的话,面试危险,嗯嗯~~~~~import java.io.*;
    class Test2{
      public static void main(String args[]){
       int[] a = new int[10];
       String s;
       try{
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       for(int i=0;i<10;i++){
       System.out.print("No"+i+":");
       s = br.readLine();
       a[i] = Integer.parseInt(s);
       }
       }catch(Exception e){
      
       }
       //排序啊
       for(int i=0;i<a.length;i++)
         System.out.println(a[i]);
      
      }
    }
      

  3.   

    import java.io.*;
    import java.util.Arrays;
    class Test2{
      public static void main(String args[]){
       int[] a = new int[10];
       String s;
       try{
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       for(int i=0;i<10;i++){
       System.out.print("No"+i+":");
       s = br.readLine();
       a[i] = Integer.parseInt(s);
       }
       }catch(Exception e){
      
       }
       Arrays.sort(a);
       for(int i=0;i<a.length;i++)
         System.out.println(a[i]);
      
      }
    }
      

  4.   

    import java.io.*;
    public class Sort
    {
    public static void main(String args[])
    {
    try
    {
    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("pleas Enter some Numbers!");
    //System.out.println(bfr.readLine());
    Sort.sortNum(bfr.readLine());
    }
    catch(Exception e)
    {
    System.err.println(e);
    }
    }
    public static void sortNum(String str)
    {
    String str1[] = str.split(" ");
    int ix[] = new int[str1.length];
    for(int i=0;i<str1.length;i++)
    {
    ix[i]=toInt(str1[i]);
    System.out.println(ix[i]);
    }
     
    }
    public static int toInt(String str)
    {
    int i = Integer.parseInt(str);
    return i;
    }
    public static 
    }//以上代码只是把读入的一行字符串转换成了整数数组,排序代码没有写
      

  5.   

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class Test1 {
    //
    public static void main(String args[])
    {
    try
    {   //这里就是接收键盘输入;
    BufferedReader bfr = new BufferedReader(
                 new InputStreamReader(System.in));
    System.out.println("pleas Enter a line  Numbers!");
    int[] Array=Test1.sortNum(bfr.readLine());
    Test1.maopaosort(Array);
    //Test1.print(Array);
    }
    catch(Exception e)
    {
    System.err.println(e);
    }
    }
    //将一行字符串转换成一个整型数组。
    public static int[]  sortNum(String string)
    {   //这里用到了分隔符比喻string.split(","),把string字符串用逗号隔开;
    String str[] = string.split(",");
    int a[] = new int[str.length];
    for(int i=0;i<str.length;i++)
    {
    a[i]=toInt(str[i]);
    }
    return  a;
    }
    //将字符转换成整型。
    public static int toInt(String str)
    {
    int i = Integer.parseInt(str);
    return i;
    }
    //打印方法。
    public static void print(int[] Array)

    int p;
    for(p = 0 ;p<Array.length; p++)
     System.out.print(Array[p]+" ");

    }
       public static void maopaosort(int[] Array)
       {
       int i;
       int j;
       int temp= 0 ;
       int count = 0;
       boolean  flag;
       
       System.out.print("排序之前的数组:");
       Test1.print(Array);//打印没排序之前的数组元素;
     
        for(i=Array.length-1 ; i>=0;i--)
        {
         flag= false;
          for(j=0;j<=i-1;j++)
          {
          if(Array[j]>Array[j+1])//交换条件。
          { flag = true;//交换了就把标量置为true;
          temp = Array[j];
              Array[j] = Array[j+1];
              Array[j+1] = temp;
          }
          }
          if(!flag) //本趟排序未发生交换,提前终止算法 
          continue; 
         count++;
        }
        
        System.out.println("\n") ;
        System.out.print("排序之后的数组:");
        Test1.print(Array);//打印排序好之后的数组;
        System.out.println("\n") ;
    System.out.println("数组中共有"+Array.length +"元素"+","+"共比较"+count+"次");
       }}