1 java
我有这段代码,我在其中循环直到用户输入 -1。但是,我收到错误:
键不能解析为变量
我之前使用过这个代码,我没有遇到任何问题,所以我不确定为什么我会遇到这个问题。
System.out.println("\nEnter a number, -1 to stop : ");
do {
int key = scan.nextInt();
int result = interpolationSearch(integerArray, key);
if (result != -1) {
System.out.println("\n"+ key +" element found at position "+ result);
}
break;
} while (key != -1); // quit
System.out.println("stop");
}
Run Code Online (Sandbox Code Playgroud)
您需要key在 do 循环之外声明,并且nextInt每次循环运行时都可以获得。break循环中也不需要语句。
System.out.println("\nEnter a number, -1 to stop : ");
int key;
do {
key = scan.nextInt();
int result = interpolationSearch(integerArray, key);
if (result != -1) {
System.out.println("\n"+ key +" element found at position "+ result);
}
} while (key != -1); // quit
System.out.println("stop");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |