Java:只输出字符串的第一个字?
我使用了Scanner var=input.nextLine(); 它适用于my name变量,但不适用于address? 这是我的密码:Java:只输出字符串的第一个字?,java,string,Java,String,我使用了Scanner var=input.nextLine(); 它适用于my name变量,但不适用于address? 这是我的密码: 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(
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
,但仅此而已,如果此字符(返回行)尚未使用,您肯定会返回到后面的行
因此input.nextLine()地址的代码>将直接执行此操作
2种选择:
int age=Integer.parseInt(input.nextLine())代码>获取所有行并转换为int
int age=input.nextInt();input.nextLine()代码>获取int并使用,然后像
答案完全在:
重复问题!!!