Java中使用方法时出现循环问题
我正在做一个关于方法的简单程序。 但我有一个问题。除了循环之外,一切都已经正常工作。 当我选择再次循环时。程序会跳过输入名称。并直接进入年份和章节。 这是代码:
public static void main(String[] args) {
do{
System.out.println("Input info:");
name=stringGetter("Name: ");
yearandsec=stringGetter("Year and section: ");
sex_code=charGetter("Sex code: " + "\n" + "[M]" + "\n" + "[F]:");
scode=intGetter("Scholarship code: ");
ccode=intGetter("Course code: ");
units=intGetter("Units: ");
fee_per_unit=doubleGetter("Fee per unit: ");
misc=doubleGetter("Miscellaneous: ");
display();
switches(scode, units, fee_per_unit, misc);
System.out.println("Another?");
dec=rew.nextInt();
}while(dec==1);
}
这是获取名称值以及年份和部分的方法:
public static String stringGetter(String ny){
String sget;
System.out.println(ny);
sget=rew.nextLine();
return sget;
}
我真的对这个问题很恼火,而且我不知道如何解决这个问题。请帮忙。谢谢
I'm doing a simple program regarding methods.
But I have one problem. Everything is already working except when looping.
When I choose to loop again. The program skips on inputting the name. And proceeds directly to the year and section.
Here's the code:
public static void main(String[] args) {
do{
System.out.println("Input info:");
name=stringGetter("Name: ");
yearandsec=stringGetter("Year and section: ");
sex_code=charGetter("Sex code: " + "\n" + "[M]" + "\n" + "[F]:");
scode=intGetter("Scholarship code: ");
ccode=intGetter("Course code: ");
units=intGetter("Units: ");
fee_per_unit=doubleGetter("Fee per unit: ");
misc=doubleGetter("Miscellaneous: ");
display();
switches(scode, units, fee_per_unit, misc);
System.out.println("Another?");
dec=rew.nextInt();
}while(dec==1);
}
Here's the method getting the value for name together with the year and section:
public static String stringGetter(String ny){
String sget;
System.out.println(ny);
sget=rew.nextLine();
return sget;
}
I'm really annoyed with this problem, and I don't have any idea on how to fix this. Please help. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个更简单、更完整的程序,它重现了该错误:
问题是,在调用
nextInt()
后,对nextLine()
的调用会读取到int(给出一个空行),不超过下一个新行。如果将 dec 更改为字符串并将 dec=rew.nextInt(); 更改为 dec=rew.nextLine(); 那么它将正常工作。这是一个完整的示例,您可以将其复制并粘贴到空白文件中以查看其是否正常工作:
您可能还需要考虑向程序添加适当的解析和验证。目前,如果用户输入无效数据,您的程序将会出现不良行为。
Here is a simpler and more complete program that reproduces the error:
The problem is that after calling
nextInt()
the call tonextLine()
reads up to the new line after the int (giving a blank line), not up to the next new line.If you change dec to a String and change
dec=rew.nextInt();
todec=rew.nextLine();
then it will work fine. Here is a complete example that you can copy and paste into a blank file to see that it works correctly:You may also want to consider adding proper parsing and validation to your program. Currently your program will behave in an undesirable way if the user enters invalid data.
该行:
正在从输入流中读取 int 值并且不处理换行符,然后当您返回到获取名称的位置时,新行仍在 Reader 的缓冲区中并被 stringGetter 消耗返回名称的空值。
更改该行以执行以下操作:
The line:
Is reading an int value from the input stream and is not processing the newline character, then when you come back to point where you get the name at which point a new line is still in the Reader's buffer and gets consumed by the stringGetter returning an empty value for name.
Change the line to do something like:
好吧,您还没有告诉我们“rew”是什么,也没有告诉我们 rew.nextInt() 的作用。是否有可能 rew.nextInt() 正在等待用户按回车键,但实际上只消耗输入的一个字符 - 以便下一次调用 rew.nextLine()< /code> (名称)立即占据该行的其余部分?我怀疑这就是发生的事情,因为您正在使用 System.in - 通常从 System.in 读取仅在您按回车键时提供任何输入。
(这可能也只是 Windows 上的一个问题 - 我想知道它是否使用 System.in 中的“\r”作为分隔符,而将“\n”留在缓冲区中。不确定.)
要测试这一点,请尝试在系统询问您是否继续时输入“1 Jon” - 我认为它将使用“Jon”作为下一个名称。
本质上,我认为当下一次调用
Scanner.nextString()
时,使用Scanner.nextInt()
将会出现问题。您最好使用 BufferedReader 并重复调用 readLine(),然后自己解析数据。Well you haven't told us what "rew" is, nor what
rew.nextInt()
does. Is it possible thatrew.nextInt()
is waiting for the user to hit return, but only actually consuming one character of the input - so that the next call torew.nextLine()
(for the name) just immediately takes the rest of that line? I suspect that's what's happening because you're usingSystem.in
- usually reading fromSystem.in
only gives any input when you hit return.(It's possible that this is also only a problem on Windows - I wonder whether it consumes the "\r" from System.in as the delimiter, leaving "\n" still in the buffer. Not sure.)
To test this, try typing in "1 Jon" when you're being asked whether or not to continue - I think it will then use "Jon" as the next name.
Essentially, I think using
Scanner.nextInt()
is going to have issues when the next call is toScanner.nextString()
. You might be better off using aBufferedReader
and callingreadLine()
repeatedly, then parsing the data yourself.