JAVA输入流问题

willem_qi 2006-06-08 09:57:21
import java.io.*;
public class a
{
public static void main(String args[]) throws java.io.IOException
{
int b=0;
byte buf[]=new byte[2];
System.out.println("请输入一个小于20的正整数:");
System.in.read(buf);
String sn=new String(buf);
int n=Integer.parseInt(sn);
System.out.println(n);
}
}
只要输入1位的数字,就会出错。请问高手怎么能解决一下?
...全文
238 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
willem_qi 2006-06-08
  • 打赏
  • 举报
回复
我是想将输入的数一整型数值记录下来~
thinker28754 2006-06-08
  • 打赏
  • 举报
回复
import java.io.*;
public class a
{
public static void main(String args[]) throws java.io.IOException
{
int b=0;
byte buf[]=new byte[2];
System.out.println("请输入一个小于20的正整数:");

System.in.read(buf);
String sn=new String(buf);
// int n=Integer.parseInt(sn);把这局去掉就可以了
System.out.println(sn);
}
}
A59229617 2006-06-08
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
System.out.println("Enter your value:");
str = br.readLine();
System.out.println("your value is :"+str);
thinker28754 2006-06-08
  • 打赏
  • 举报
回复
我又改了改这样就可以了:
import java.io.*;
public class a
{
public static void main(String args[]) throws java.io.IOException
{
int b=0;
byte buf[]=new byte[2];
System.out.println("请输入一个小于20的正整数:");
// int s=System.in.read();
System.in.read(buf);
String sn=new String(buf);
int n=0;
if(buf[1]==13)
{
n=buf[0]-48;
}
else
{
n=Integer.valueOf(sn);
}


System.out.println(n);
}
}

62,614

社区成员

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

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