扫描程序未接受其他字符串的输入并跳过它 [英] Scanner is not taking input of another string and skipping it
本文介绍了扫描程序未接受其他字符串的输入并跳过它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么扫描仪不接受另一个字符串的输入并跳过它?我不明白,这是我的代码:
Why scanner is not taking input of another string and skepping it? I cant understand, here is my code:
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name;
String address;
int age;
System.out.println("Enter your name");
name = s.nextLine();
System.out.println("My name is :" + name);
System.out.println("Enter your age");
age = s.nextInt();
System.out.println("My age is :" + age);
System.out.println("Enter your address");
address = s.nextLine();
System.out.println("My address is :" + address);
}
}
输出:
输入您的姓名
dk
我的名字是:dk
输入您的年龄
22
我的年龄是:22
输入您的地址
我的地址是:
Enter your name
dk
My name is :dk
Enter your age
22
My age is :22
Enter your address
My address is :
推荐答案
这很简单.您可以在age = s.nextInt()
之后使用s.nextLine();
;
It is simple. You can use s.nextLine();
after the age = s.nextInt()
;
扫描程序提供了一种方法nextInt()
来请求用户输入,并且不使用最后一个换行符(\n
).
Scanner provides a method nextInt()
to request user input and does not consume the last newline character (\n
).
System.out.println("Enter your age");
age = s.nextInt();
s.nextLine(); // consumes the \n character
System.out.println("My age is :" + age);
这篇关于扫描程序未接受其他字符串的输入并跳过它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文