上次一个兄弟帮我分析了那个程序,还做了修改.可是运行时结果不对大家再琢磨一下吧
import       java.io.*;   
import       java.util.*;   
public class Array   
{   
public static void main(String aa[])throws Exception   
{   
int       ch=0;   
int       pos=0;   
byte       buf[]=new byte[255];   
String       str;   
System.out.println("请输入10个数字");   
while(true)   //你的本意是将输入的数字存入到数组buf中,但是实际上,你将空格也存入了数组中,空格的ASC码是32. 
{//所以,你输入10个数字后,且每个数字间有一个空格,则你最终的pos的值是19. 
ch=System.in.read();   //注意,ch接受到的值实际上是数字的ASC码,要减去48.(48是0的ASC码,在输出处减即可) 
if(ch=='\r')   
break;   
if(ch=='\n')   
System.out.write(buf);   
if(ch!=32)buf[pos++]=(byte)ch;   //加上一个if(ch!=32),空格的ASC码是32,就可以不将空格计算在内,只存入数字了. 
}   
System.out.println(pos); 
Arrays.sort(buf);   
System.out.println(buf[0]);   
System.out.println(buf[pos-1]);//输入了10个数,pos的值是10,但最后一个数在数组中的脚标其实是9,因为数组脚标是从0开始的 
//for(int   i=0;i <pos;i++)System.out.println(buf[i]-48);//用for循环将排完序后的数组输出 
}   
}

解决方案 »

  1.   

    这个应该能满足你的要求:import java.io.*;
    import java.util.*;
    class ArrayTest
    {
    public static void main(String [] args)
    {
    int ch = 0;
    int pos = 0;
    byte [] buf = new byte[255];
    System.out.println("请输入数字:");
    while (true)
    {
    try
    {
    ch = System.in.read();
    if (ch < 0 || (char)ch == '\n')//输入流是否结束
    break;
    if (ch < 48 || ch > 57) //输入下一个元素是否是数字.
    continue; //不是数字就从新读取一个.
    buf[pos++] = (byte)ch; //读取到的数字存入数组.
    }
    catch(java.io.IOException e)
    {
    break;
    }
    }
    System.out.println("你输入了"+pos+"数字!");
    System.out.print("你输入的数字是:");
    for(int i=0;i<pos;i++)
    {
    System.out.print((char)buf[i]);//打印出数组中的数字.
    }
    Arrays.sort(buf,0,pos); //对buf进行排序,排序的范围是0到pos-1.
    //如果不加范围参数就会对255个元素排序,输出时你就会什么也看不到
    System.out.println(); //换行
    System.out.print("排序后的数字序列是:");
    for(int j=0;j<pos;j++)
    {
    System.out.print((char)buf[j]);//打印出数组中的数字.
    }
    }
    }
      

  2.   


    public static void main(String[] args)throws Exception {
         System.out.println("请输入10个数字");
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String content = br.readLine();
         String [] result = content.split(" ");
         Arrays.sort(result);
         for(int i=0;i<result.length;i++){
    System.out.println(result[i]);
     }
    }