我刚学编程,如果问题很简单,请原谅,但是我似乎找不到真正的答案了。我正在使用一个扫描器对象来获取猜数字游戏中用户的输入。扫描器在我的主方法中声明,并将在另一个方法中使用(但该方法将被多次调用)。
我尝试将其声明为静态变量,但是Eclipse对此表示不满并且无法运行。
public static void main(String[] args) {
int selection = 0;
Scanner dataIn = new Scanner(System.in);
Random generator = new Random();
boolean willContinue = true;
while (willContinue)
{
selection = GameList();
switch (selection){
case 1: willContinue = GuessNumber(); break;
case 2: willContinue = GuessYourNumber(); break;
case 3: willContinue = GuessCard(); break;
case 4: willContinue = false; break;
}
}
}
public static int DataTest(int selectionBound){
while (!dataIn.hasNextInt())
{
System.out.println("Please enter a valid value");
dataIn.nextLine();
}
int userSelection = dataIn.nextInt;
while (userSelection > selectionBound || userSelection < 1)
{
System.out.println("Please enter a valid value from 1 to " + selectionBound);
userSelection = dataIn.nextInt;
}
return userSelection;
}