问题描述
package Lessons;
import java.util.Scanner;
public class Math {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("please enter the first number of your question! ");
int a1 = s.nextInt();
System.out.print("So you chose " + a1);
System.out.println(" Choose ur second number");
int a2 = s.nextInt();
System.out.print("You chose " + a2 + " for your second number");
System.out.print(" Now enter ur operator ");
String b1 = s.nextLine();
if (b1.equalsIgnoreCase("-")) {
System.out.println("You chose " + b1 + "as ur operator");
}
}
}
我做不明白为什么第15行不接受任何输入,任何帮助将不胜感激:3
I do not understand why line 15 does not take any input, any help would be appreciated:3
输出
由于某种原因,输出在最后一行结束并停止,并且不接受任何信息!
Output ends and stops at the last line for some reason and does not take any information!
推荐答案
您需要致电 in.nextLine()
就在您调用 in.nextInt()的行之后
原因只是要求下一个整数不会消耗输入中的整行,因此您需要通过调用 in.nextLine()
来跳转到输入中的下一个换行符。
You need to call in.nextLine()
right after the line where you call in.nextInt()
The reason is that just asking for the next integer doesn't consume the entire line from the input, and so you need skip ahead to the next new-line character in the input by calling in.nextLine()
.
int a2 = s.nextInt();
s.nextLine();
每次调用一个方法后需要获取新行时,这几乎必须完成不消耗整行,例如当你调用 nextBoolean()
等时。
This pretty much has to be done each time you need to get a new line after calling a method that doesn't consume the entire line, such as when you call nextBoolean()
etc.
这篇关于为什么我的Java扫描程序不接受输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!