import java.io.*;
public class IOTest
{
public static void main (String[] args )
{
int b = 4;
System.out.print ("Input a integer:");
int a;
char[] c = new char[1];
try {
a = System.in.read();
if ((a > 57) || (a< 48)) 
System.out.println("输入错误,请输入数字"); 
c[0] = (char)a;
System.out.println (" " + (Integer.parseInt(new String(c)) + b));
} catch (IOException e){
System.out.println("Error of IO");
}
   }
}我也才开始学java,不知哪位高手有更好的方法?

解决方案 »

  1.   

    System.in.read()的返回不是String型的
      

  2.   

    import java.io.*;
    public class IOTest
    {
       public static void main ( String [ ] args )
       {
          int b;
          System.out.print ( "Input a integer:" );
          int a = Integer.parseInt ( System.in.read ( ) );
          System.out.println ( a + b );
       }
    }
      

  3.   

    import java.io.*;
    public class IOTest
    {
       public static void main ( String [ ] args ) throws IOException
       {
          int a=0,b=5; //要初始化的!
          System.out.print ( "Input a integer:" );
          try
          {
            a= Integer.parseInt( System.in.read( )); //这个可能会有异常抛出
          }
          catch(NumberFormationException e)
          { 
             System.out.println("请确信输入的是数字!");
           }  
          System.out.println ( a + b );
       }
    }试试吧! 应该可以了
      

  4.   

    不好意思,上面写错了!import java.io.*;
    public class IOTest
    {  public static String keyin()
      { 
        String s=" ";
        try
        {
    BufferedReader one=new BufferedReader(new InputStreamReader(System.in));
    s=one.readLine();
        }
       catch(IOException e) {System.out.println("There was wrong !");}
     
       return s; 
      }
       public static void main ( String [ ] args ) throws IOException
       {
          int a=0,b=5; //要初始化的!
          System.out.print ( "Input a integer:" );
          try
          {
            a= Integer.parseInt(keyin( )); //这个可能会有异常抛出
          }
          catch(NumberFormationException e)
          { 
             System.out.println("请确信输入的是数字!");
           }  
          System.out.println ( a + b );
       }
    }
    这下试试吧!刚才实在是不好意思! 忘记了!
      

  5.   

    非常谢谢 DainelLee(现在开始学java)
    我又学到了东西!