import java.io.*;
public class a
{
public static void main(String args[]) throws java.io.IOException
    {
     int b=0;
     byte buf[]=new byte[2];
        System.out.println("请输入一个小于20的正整数:");
        System.in.read(buf);
        String sn=new String(buf);
        int n=Integer.parseInt(sn);
        System.out.println(n);
    }

只要输入1位的数字,就会出错。请问高手怎么能解决一下?

解决方案 »

  1.   

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String str = null; 
    System.out.println("Enter your value:"); 
    str = br.readLine(); 
    System.out.println("your value is :"+str); 
      

  2.   

    import java.io.*;
    public class a
    {
    public static void main(String args[]) throws java.io.IOException
        {
         int b=0;
         byte buf[]=new byte[2];
            System.out.println("请输入一个小于20的正整数:");
          
             System.in.read(buf);
             String sn=new String(buf);
          //  int n=Integer.parseInt(sn);把这局去掉就可以了
            System.out.println(sn);
        }

      

  3.   

    我又改了改这样就可以了:
    import java.io.*;
    public class a
    {
    public static void main(String args[]) throws java.io.IOException
        {
         int b=0;
         byte buf[]=new byte[2];
            System.out.println("请输入一个小于20的正整数:");
          //  int s=System.in.read();
             System.in.read(buf);
             String sn=new String(buf);
             int n=0;
             if(buf[1]==13)
             {
              n=buf[0]-48;
             }
             else
             {
              n=Integer.valueOf(sn);
             }
            
          
           System.out.println(n);
        }