[英]Java: only first word of string being output?
我用过Scanner var = input.nextLine(); 它适用于我的名称变量,但不适用于地址吗? 这是我的代码:
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("How old are you?");
int age = input.nextInt();
System.out.println("Where do you live?");
String address = input.nextLine();
System.out.println("Your name is " + name + ".\n You are " + age + " years old." + "\n You live at " + address + ".");
这是它的显示内容:
What is your name?
Yassine assim
How old are you?
17
Where do you live?
Your name is Yassine assim.
You are 17 years old.
You live at .
int age = input.nextInt();
消耗您键入的int
,仅此而已,如果没有使用此char(返回行),则一定要返回行
所以input.nextLine();
地址将直接执行
2种选择:
int age = Integer.parseInt(input.nextLine());
取所有行并转换为int
int age = input.nextInt(); input.nextLine();
接受int并使用,然后消耗诸如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.