java吧 关注:1,241,945贴子:12,712,914
  • 9回复贴,共1

这个程序有点结果怪异,求高手指点

只看楼主收藏回复

import java.io.IOException;
public class A { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("请输入一个正整数:");
int n = System.in.read(); System.out.println("n=" + n);
}
} }
结果截图:




IP属地:广东1楼2012-08-20 22:18回复


    IP属地:广东2楼2012-08-20 22:19
    回复
      查查文档看看。


      IP属地:北京来自Android客户端3楼2012-08-20 22:23
      回复

        怎么今天沉的这么快的,一发在第一页就看不到了


        IP属地:广东4楼2012-08-20 22:26
        回复
          必然打印出来是哈希值而已。死循环。包括那个53的3也是一个哈希值。


          7楼2012-08-20 22:51
          回复
            主要是LZ用了System.in.read()这个方法,这个方法主要是
            Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer
            返回的是程序读到字符的ASCII码号,而且是一个字节一个字节地读,比如“5”的ASCII码号为53,后面的13和10是回车和软回车的ASCII码号。
            LZ可以试试下面的方法
            try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str=br.readLine().trim();
            System.out.println("n="+str);
            }catch(java.io.IOException i){}
            其中的BufferedReader类中的readLine方法可一次读一行,返回字符串。
            注:不要忘了在开头加import java.io.BufferedReader;


            8楼2012-08-20 23:35
            回复