System.out.print("What is your name ?"); name = in.nextLine(); System.out.print("What is your age ?"); age = in.nextInt(); System.out.printf("Hello %s , age %d\n", name , age); //结果:Hello Wang , age 24 System.out.print("What is your age ?"); age = in.nextInt(); in.nextLine();//读取换行符 System.out.print("What is your name ?"); name = in.nextLine(); System.out.printf("Hello %s , age %d\n", name , age); //结果:Hello , age 24,不允许输入姓名 //Scanner类并不像我们那样将输入视为多行,反而,他获得的是一个“字符流” //nextInt读取字符45,nextLine读取字符会遇到换行符,返回空字符串 //先读取数字,在读取当前行余下的内容(仅仅是一个换行符) //先读取String再读取int一切正常