【发布时间】:2017-10-30 19:12:12
【问题描述】:
我正在用一个名为DealOrNoDeal 的类为Java 游戏制作一个简单的方法。
这是一种非常简单的方法,可以在两个边界之间从用户那里获取 int 输入。但由于某种原因,它抛出了java.lang.NullPointerException。
这是我的代码:
import java.util.Scanner;
import java.util.Arrays;
public class DealOrNoDeal {
public Scanner scanner;
public static void main(String[] args) {
new DealOrNoDeal().run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
int[] values = {1, 20, 100, 1000, 2000, 5000};
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
int keuze = getIntBetweenBounds(1, 6);
}
private int getIntBetweenBounds(int lowerBound, int upperBound) {
int result = scanner.nextInt();
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
while (result < lowerBound || result > upperBound) {
System.out.println("Please select the suitcase that you would like to keep (1-6): ");
result = scanner.nextInt();
}
return result;
}
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
堆栈跟踪是什么意思?
-
把
Scanner scanner = new Scanner(System.in);改成this.scanner = new Scanner(System.in);怎么样 -
@LoekRomer 堆栈跟踪是您返回的完整异常
-
@rafathanx!现在可以了!