For loop scanner.nextInt()的连续实现问题
我正在尝试使用scanner.nextInt()分配两个变量L和R。赋值后,这些变量立即用于for循环。我的问题是扫描器流在命令行中保持打开状态,并且程序从不执行后续的for循环。下面是一个片段:For loop scanner.nextInt()的连续实现问题,for-loop,input,integer,java.util.scanner,For Loop,Input,Integer,Java.util.scanner,我正在尝试使用scanner.nextInt()分配两个变量L和R。赋值后,这些变量立即用于for循环。我的问题是扫描器流在命令行中保持打开状态,并且程序从不执行后续的for循环。下面是一个片段: System.out.println("Enter the Left and Right endpoints: "); L = sc.nextInt(); R = sc.nextInt(); for (int i = L; i < R; i += resolution) { do
System.out.println("Enter the Left and Right endpoints: ");
L = sc.nextInt();
R = sc.nextInt();
for (int i = L; i < R; i += resolution) {
double a = i, b = resolution+i;
if (poly(C, a)*poly(C, b) < 0) {
double mid = findRoot(C, a, b, tolerance);
System.out.println(mid);
if (Math.abs(poly(C, mid)) < threshold){
System.out.println("Root found at: "+mid);
numCount = 1;
}
} else if (poly(D, a)*poly(D, b) < 0) {
double mid = findRoot(D, a, b, tolerance);
if(Math.abs(poly(C, mid)) < threshold) {
System.out.println("Root found at: "+mid);
numCount = 2;
} else {
numCount = 3;
System.out.println("No roots were found in the specified range.");
}
System.out.println("numCount is "+numCount); break;
}
}
System.out.println(“输入左右端点:”);
L=sc.nextInt();
R=sc.nextInt();
对于(int i=L;i
您如何知道循环未执行?您传入什么输入?如果执行,循环将打印一条语句。在UNIX命令行中,流对整数输入保持打开状态。我必须使用CTRL-C退出程序执行。似乎对我有用。是否存在造成此问题的特定输入?或者您包含的代码片段过于简化了问题?我猜要么您的循环没有结束,要么您在某处有另一个nextSomething
调用。什么是分辨率
?所有输入都会产生此问题。事实上,当键入非整数时,它不会返回错误。