返回

java-扫描仪在使用next()或nextFoo()后跳过nextLine()?

发布时间:2022-03-15 20:55:11 312
# 移动端

我用的是Scanner方法nextInt()nextLine()用于读取输入。

看起来是这样的:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)

问题是,在输入数值后input.nextLine()跳过第二个input.nextLine()执行,因此我的输出如下所示:

Enter numerical value
3   // This is my input
Enter 1st string    // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string    // ...and this line is executed and waits for my input

我测试了我的应用程序,看起来问题在于使用input.nextInt().如果我删除它,那么两个string1 = input.nextLine()string2 = input.nextLine()按照我的要求执行。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像