请问各位大侠如何在Java中使用类似scanf的输入函数?我找了n久都米找到

解决方案 »

  1.   

    不时有个函数System.in吗  你去看下API啊
      

  2.   

    http://java.d9t3.com/ViewDownInfo.asp?ID=10
      

  3.   

    多谢各位前辈的指点 小弟经过研究自己做了一个类 用下来感觉还不错 现在贴上来给大家一起分享一下!还希望各位多多指点啊!
    import java.io.*;class ConsoleInPut
    {
         private BufferedReader in;//定义输入输出流对象
         private String str;//定义String变量以便接受输入字符串
         private char []ch=new char[1];//定义char[]变量以便接受输入字符串
         private int num;//定义int变量用途同上
         private float fl;//定义float变量
         private double dou;//定义double变量
         
    /*构造函数 实例化输入流对象  实例化类的属性*/     public ConsoleInPut()
         {
          this.in = new BufferedReader(new InputStreamReader(System.in));//实例化输入流对象
          this.str="";//以下为初始化各类型的变量 以便处理I/O异常
     this.ch[0]='0';
          this.num=0;
          this.fl=0f;
          this.dou=0d;
         }
         
    /*输入字符串数据方法*/     public String strInPut() throws NumberFormatException
         {
         boolean b=true;
    do
         {
    try
         {
         this.str= in.readLine();
         b=false;
    }
         catch(IOException e)
         {
         System.out.println("程序发生输入输出流的异常!请重试!");
         b=true;
    }
    catch(NumberFormatException e1)
    {
    System.out.println("输入的数据类型不匹配!请重试!");
    b=true;
    }
    }
         while (b);
    return this.str;
         }

    /*输入单个字符数据方法*/  public char charInPut() throws NumberFormatException
    {
    boolean b=true;
    do
    {
    try
    {
    this.str=in.readLine();
    this.ch=this.str.toCharArray();
    b=false;
    }
    catch (IOException e)
    {
    System.out.println("程序发生输入输出流的异常!请重试!");
         b=true;
    }
    catch(NumberFormatException e1)
    {
    System.out.println("输入的数据类型不匹配!请重试!");
    b=true;
    }
    }
    while (b);
    return this.ch[0];
     }
         
    /*输入int类型数据方法*/     public int intInPut() throws NumberFormatException
         {
          boolean b=true;
     do
     {
     try
         {
         this.str=in.readLine();
         this.num=Integer.parseInt(this.str);//封装类Integer的方法parseInt将String转换成int
         b=false;
    }
             catch(IOException e)
         {
         System.out.println("程序发生输入输出流的异常!请重试!");
         b=true;
    }
    catch(NumberFormatException e1)
    {
    System.out.println("输入的数据类型不匹配!请重试!");
    b=true;
    }
    }
     while (b);
     return this.num;
         }
         
    /*输入float类型数据方法*/     public float floatInPut() throws NumberFormatException
         {
         boolean b=true;
    do
    {
     try
          {
         this.str=in.readLine();
         this.fl=Float.parseFloat(this.str);//封装类Float的方法parseFloat将String转换成float
         b=false;
     }
         catch(IOException e)
         {
         System.out.println("程序发生输入输出流的异常!请重试!");
         b=true;
         }
    catch(NumberFormatException e1)
    {
    System.out.println("输入的数据类型不匹配!请重试!");
    b=true;
    }
    }
    while (b);
    return this.fl;
         }
         
    /*输入double类型数据方法*/     public double doubleInPut() throws NumberFormatException
         {
          boolean b=true;
     do
     {
     try
          {
         this.str=in.readLine();
         this.dou=Double.parseDouble(this.str);//封装类Double的方法parseDouble将String转换成double
         b=false;
     }
         catch(IOException e)
         {
         System.out.println("程序发生输入输出流的异常!请重试!");
         b=true;
        }
    catch(NumberFormatException e1)
    {
    System.out.println("输入的数据类型不匹配!请重试!");
    b=true;
        }
     }
     while (b);
     return this.dou;
         }
    }