我是编程新手,所以如果对此有一个非常简单的答案,我深表歉意,但实际上我似乎找不到任何东西。我在猜数字游戏中使用扫描仪对象进行用户输入。扫描仪在我的 main 方法中声明,并将在一个其他方法中使用(但该方法将在整个地方被调用)。
我试过将它声明为静态的,但 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;
}