System.out.print("Name : ");
String name = in.nextLine();
System.out.print("Age : ");
int age = in.nextInt();
System.out.print("City : ");
String city = in.nextLine();
Run Code Online (Sandbox Code Playgroud)
输出将是:
名称:测试
年龄:20岁
建立成功
当我调试它们时,它不会读取"city"的用户输入.但是当我将"age"的数据类型更改为string时,它将会读取.如何通过系统读取城市的用户输入,将年龄的数据类型保持为int?
由于在读取年龄后缓冲区中仍有新行字符,请将其更改为..
System.out.print("Name : ");
String name = in.nextLine();
System.out.print("Age : ");
int age = in.nextInt();
//add
in.nextLine();
System.out.print("City : ");
String city = in.nextLine();
Run Code Online (Sandbox Code Playgroud)