一个简单的输入问题

zhangmingpowerman 2007-07-03 08:36:17
请问各位大侠如何在Java中使用类似scanf的输入函数?我找了n久都米找到
...全文
293 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
joejoe1991 2007-07-17
  • 打赏
  • 举报
回复
Scanner BufferedReader
CrazyGou 2007-07-17
  • 打赏
  • 举报
回复
脑残...
zhangmingpowerman 2007-07-17
  • 打赏
  • 举报
回复
多谢各位前辈的指点 小弟经过研究自己做了一个类 用下来感觉还不错 现在贴上来给大家一起分享一下!还希望各位多多指点啊!
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;
}
}
yondy 2007-07-03
  • 打赏
  • 举报
回复
jdk1.5以后有个Scanner
stefli 2007-07-03
  • 打赏
  • 举报
回复
System.in
自己就搜索这个
bsfxli 2007-07-03
  • 打赏
  • 举报
回复
up
tdy1234 2007-07-03
  • 打赏
  • 举报
回复
不时有个函数System.in吗 你去看下API啊
zhangmingpowerman 2007-07-03
  • 打赏
  • 举报
回复
多谢各位大侠的指点 小弟已经豁然开朗啦
weiqiwei 2007-07-03
  • 打赏
  • 举报
回复
http://java.d9t3.com/ViewDownInfo.asp?ID=10

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧