新人求教,关于输入的问题

wu13511 2012-08-13 05:13:20
public static void main(String[] args) throws IOException{
System.out.println("请输入一个小写字母:");
int read=System.in.read();
}
和public static void main(String[] args){
Scanner scan=new Scanner(System.in);
string i=scan.nextline();
....
}
这两种输入有什么区别啊,新人求教啊,
...全文
97 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky254541843 2012-08-13
  • 打赏
  • 举报
回复
System.in系统预设的为BufferedInputStream
System.in.read()读取的是你输入字符串中的第一个字节值
而Scanner scan=new Scanner(System.in);
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}

Scanner内部会将其包装为字符流
scan.nextline();读取的就是你输入的一行字符串
Sammie 2012-08-13
  • 打赏
  • 举报
回复
System.in.read() 表示的是你输入的Acsii值

Scanner此类是用于输出你所输入的值。

看下Scanner类中nextLine源码你就知道了。

public String nextLine() {
if (hasNextPattern == linePattern())
return getCachedResult();
clearCaches();

String result = findWithinHorizon(linePattern, 0);
if (result == null)
throw new NoSuchElementException("No line found");
MatchResult mr = this.match();
String lineSep = mr.group(1);
if (lineSep != null)
result = result.substring(0, result.length() - lineSep.length());
if (result == null)
throw new NoSuchElementException();
else
return result;
}

62,620

社区成员

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

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