代码仅在第一次输入时。在第二个iteraton期间,它在输出中显示空白区域,如下所示。
代码是:
char gender;
String name;
Scanner in=new Scanner(System.in);
do{
System.out.println("Enter your name: ");
name=in.nextLine();
gender=in.next().charAt(0);
System.out.println("Name: " + name);
}while(!(name.equals("user")));
输出结果是:
Enter your name:
abc
Enter your gender(M/F):
m
Name: abc
Enter your name:
Enter your gender(M/F):
m
Name:
Enter your name:
Enter your gender(M/F):
m
Name:
Enter your name:
Enter your gender(M/F):
为什么会这样?
答案 0 :(得分:0)
据我所知,用户将输入姓名,您只需选择姓名吗?
如果我理解为真,那就可以解决:(我们假设他/她有一个名字。
Scanner scan = new Scanner(System.in);
String fullname, name, surname;
fullname = scan.nextLine();
name = fullname.substring(0,fullname.indexOf(' '));
surname = fullname.substring(fullname.indexOf(' ')+1);
您可以根据需要输入任何do-while循环