[英]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.