java语言中输入问题

emailed 2008-03-31 09:13:05
java的io好头疼阿

当从键盘接受各种类型的数据应该怎样最为简单呢?比如char ,int, double。。。。
麻烦给出主要的代码段吧,谢谢了
...全文
86 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kokobox 2008-04-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fuyou001 的回复:]
BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
[/Quote]

正解,支持fuyou001
霜之哀伤 2008-04-01
  • 打赏
  • 举报
回复
下面是java输入输出基本类Input类的源代码:
import java.io.*;
class Input
{static InputStreamReader in;
static BufferedReader reader;
static
{in=new InputStreamReader(System.in);
reader=new BufferedReader(in);
}


static String readString()
{String s="";
try
{ s=reader.readLine();

}

catch(IOException e)
{System.out.println(e);
System.exit(0);
}
return s;
}

static char readChar()
{char ch='a';
try
{
String s=readString();
ch=s.charAt(0);

}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return ch;
}


static int readInt()
{String s=readString();
int i=0;
try{
i=Integer.parseInt(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return i;
}
static double readDouble()
{String s=readString();
double d=0.0;
try
{d=Double.parseDouble(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return d;
}
static float readFloat()
{
String s=readString();
float f=0.0f;
try
{
f=Float.parseFloat(s);
}
catch(Exception e)
{ System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return f;
}
}

用法举例,从键盘输入十个整数:
class InoutData
{public static void main(String args[])
{ int a[]=new int[10];
for(int i=0;i<10;i++)
{ System.out.println("请输入数");
a[i]=Input.readInt();
}
for(int i=0;i<10;i++)
System.out.println("a["+i+"]="+a[i]);
}
}
霜之哀伤 2008-04-01
  • 打赏
  • 举报
回复
太基本了,不写了,转一个:

下面是java输入输出基本类Input类的源代码:
import java.io.*;
class Input
{static InputStreamReader in;
static BufferedReader reader;
static
{in=new InputStreamReader(System.in);
reader=new BufferedReader(in);
}


static String readString()
{String s="";
try
{ s=reader.readLine();

}

catch(IOException e)
{System.out.println(e);
System.exit(0);
}
return s;
}

static char readChar()
{char ch='a';
try
{
String s=readString();
ch=s.charAt(0);

}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return ch;
}


static int readInt()
{String s=readString();
int i=0;
try{
i=Integer.parseInt(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return i;
}
static double readDouble()
{String s=readString();
double d=0.0;
try
{d=Double.parseDouble(s);
}
catch(Exception e)
{System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return d;
}
static float readFloat()
{
String s=readString();
float f=0.0f;
try
{
f=Float.parseFloat(s);
}
catch(Exception e)
{ System.out.println("输入的数据类型不对,程序将退出");
System.exit(0);
}
return f;
}
}

用法举例,从键盘输入十个整数:
class InoutData
{public static void main(String args[])
{ int a[]=new int[10];
for(int i=0;i<10;i++)
{ System.out.println("请输入数");
a[i]=Input.readInt();
}
for(int i=0;i<10;i++)
System.out.println("a["+i+"]="+a[i]);
}
}
fuyou001 2008-04-01
  • 打赏
  • 举报
回复
BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
wenzheng38 2008-04-01
  • 打赏
  • 举报
回复
使用对象流ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream(new File("****")));

obj.writeInt(123);
obj.writeChars("afhaf");
obj.writeDouble(12.2223);
emailed 2008-03-31
  • 打赏
  • 举报
回复
给我写个程序吧,就是输入各种数据的都集中起来,我刚学java,谢谢了阿
  • 打赏
  • 举报
回复
为什么不直接接收字符串 然后再进行转换呢

62,623

社区成员

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

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