我有这个代码
System.out.println("Enter surname to search for:");
String choice2;
choice2= sc.nextLine();
在我看来,这应该可行,但由于某种原因,程序没有暂停等待用户输入姓氏,下一行代码正好在它之后执行。请问这段代码有问题吗?
This code is correct , and already wait until the user enter the name , but this code will not pausing in case of you press the Enter key before you enter the name because you use sc.nextLine() , but sc.next() will wait you until you enter the text
这将尝试choice2
使用“Hello”之类的东西打印值:
System.out.println("Enter surname to search for:");
String choice2;
choice2= sc.nextLine();
System.out.println("Hello "+choice2);
让我们看看这条线是否正在打印,但它不应该。
如果它的打印Hello
并且没有其他任何东西意味着您输入了额外的新行,那么只需choice2= sc.nextLine();
在下面再次调用一个条件 ifchoice2 is ""
。
System.out.println("Enter surname to search for:");
String choice2;
choice2= sc.nextLine();
while(choice2.equels("").trim()){
choice2= sc.nextLine();
}
有关更多详细信息,请访问以下站点。