在使用Java的Scanner类接收多个输入时遇到问题。

huangapple go评论58阅读模式
英文:

facing issue in java with scanner class while taking multiple input

问题

我正在尝试从用户那里获取不同类的多个输入如果我只使用String类型我的代码可以正常工作但是当我使用任何其他类比如intfloat或其他任何类它会忽略接下来的String输入为什么会发生这种情况有人可以帮帮我

Scanner obj = new Scanner(System.in);
String a, b, c;
int x, y, z;
System.out.print("请输入任意字符串:");
a = obj.nextLine();
System.out.print("请输入任意整数:");
x = obj.nextInt();
System.out.print("请输入任意内容:");    // 在获取完整数输入后,它会忽略接下来的字符串输入
obj.nextLine();                     // 输入
System.out.print("请输入任意内容:");
c = obj.nextLine();
英文:

I am trying to take multiple input from user of different classes, if I am working only with String type my code work fine but when I am using any other class like int, flot or any other it ignore the next String input why is this happening can any one help me.

Scanner obj = new Scanner(System.in);
String a,b,c;
int x,y,z;
System.out.print("Enter any string: ");
a = obj.nextLine();
System.out.print("enter any int: ");
x = obj.nextInt();                                                                                             
System.out.print("Enter any thing: ");    //after getting input for int it ignore next string 
b = obj.nextLine();                       input
System.out.print("Enter any thing: ");
c = obj.nextLine();

答案1

得分: 0

在调用obj.nextInt()之后,由于用户在输入数字后按下了回车键,因此仍然会在输入缓冲区中保留来自回车键的行尾符号。这是因为nextInt()只读取数字字符。您需要通过调用obj.nextLine()来跳过这些字符,并忽略其结果。然后,您可以继续向用户请求额外的输入。

以下代码段实现了您的预期效果:

Scanner obj = new Scanner(System.in);
String a, b, c;
int x, y, z;
System.out.print("请输入任意字符串:");
a = obj.nextLine();
System.out.print("请输入任意整数:");
x = obj.nextInt();
obj.nextLine();
System.out.print("请输入任意内容:");    //获取整数输入后,会忽略下一个字符串
b = obj.nextLine();
System.out.print("请输入任意内容:");
c = obj.nextLine();

System.out.println("1: " + b);
System.out.println("2: " + c);

示例运行:

请输入任意字符串:anystring
请输入任意整数:12345
请输入任意内容:anything
请输入任意内容:more anything
1: anything
2: more anything
英文:

After you call obj.nextInt(), you still have the EOL from the Return that the user pressed after entering the number sitting in the input buffer, because nextInt() only read the numeric characters. You have to skip those characters by calling obj.nextLine() and just ignoring the result. Then you can go on and ask for additional input from the user.

This gives you what you expected:

Scanner obj = new Scanner(System.in);
String a,b,c;
int x,y,z;
System.out.print("Enter any string: ");
a = obj.nextLine();
System.out.print("enter any int: ");
x = obj.nextInt();
obj.nextLine();
System.out.print("Enter any thing: ");    //after getting input for int it ignore next string
b = obj.nextLine();
System.out.print("Enter any thing: ");
c = obj.nextLine();

System.out.println("1: " + b);
System.out.println("2: " + c);

Sample run:

Enter any string: anystring
enter any int: 12345
Enter any thing: anything
Enter any thing: more anything
1: anything
2: more anything

huangapple
  • 本文由 发表于 2020年10月27日 01:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定