一个简单的输入问题 请问各位大侠如何在Java中使用类似scanf的输入函数?我找了n久都米找到 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不时有个函数System.in吗 你去看下API啊 http://java.d9t3.com/ViewDownInfo.asp?ID=10 多谢各位前辈的指点 小弟经过研究自己做了一个类 用下来感觉还不错 现在贴上来给大家一起分享一下!还希望各位多多指点啊!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; }} 急求:Struts-menu的背景色问题 这道IO题目,怎么做最优化呢? SocketInputStream 的问题 【内网安全问题】偷什么别偷良心!(怎么防止公司内部员工泄露?) 类动态加载的又一问题! 请教各位大神一个关于java swing的问题 用jcreator的朋友请进 为什么我用ResultSet中的last(),absolute()就出错 javase求助贴,大家能帮我看看哪错了吗?不能绘制出背景图片 数据库统计怎么偶尔统计出多一个 java 计算时间差的问题 一个简单的程序问题哦!!!
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;
}
}