[code=Java][/code]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();
....
}
这两种输入有什么区别啊,新人求教啊,
------解决方案--------------------------------------------------------
System.in.read() 表示的是你输入的Acsii值
Scanner此类是用于输出你所输入的值。
看下Scanner类中nextLine源码你就知道了。
- Java code
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; }
------解决方案--------------------------------------------------------
System.in系统预设的为BufferedInputStream
System.in.read()读取的是你输入字符串中的第一个字节值
而Scanner scan=new Scanner(System.in);
- Java code
public Scanner(InputStream source) { this(new InputStreamReader(source), WHITESPACE_PATTERN);}