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();
....
}
这两种输入有什么区别啊,新人求教啊,

解决方案 »

  1.   

    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;
        }
      

  2.   

    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();读取的就是你输入的一行字符串