Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在方法内部使用扫描仪_Java_Methods_Input - Fatal编程技术网

Java 在方法内部使用扫描仪

Java 在方法内部使用扫描仪,java,methods,input,Java,Methods,Input,我是编程新手,所以如果有一个非常简单的答案,我很抱歉,但我似乎找不到任何实际的答案。我正在使用一个扫描器对象在猜你的数字游戏中进行用户输入。scanner在我的main方法中声明,并将在一个单独的其他方法中使用(但该方法将被到处调用) 我曾尝试将其声明为静态,但eclipse对此有点不适应,无法运行 public static void main(String[] args) { int selection = 0; Scanner dataIn = new Scanner(S

我是编程新手,所以如果有一个非常简单的答案,我很抱歉,但我似乎找不到任何实际的答案。我正在使用一个扫描器对象在猜你的数字游戏中进行用户输入。scanner在我的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;
}
publicstaticvoidmain(字符串[]args){
整数选择=0;
扫描仪数据输入=新扫描仪(System.in);
随机生成器=新随机();
布尔值willContinue=true;
while(将继续)
{
选择=游戏列表();
开关(选择){
案例1:willContinue=GuessNumber();break;
案例2:willContinue=GuessYourNumber();break;
案例3:willContinue=GuessCard();break;
案例4:willContinue=false;break;
}
}
}
公共静态int-DataTest(int-selectionBound){
而(!dataIn.hasnetint())
{
System.out.println(“请输入有效值”);
dataIn.nextLine();
}
int userSelection=dataIn.nextInt;
while(userSelection>selectionBound | | userSelection<1)
{ 
System.out.println(“请输入一个从1到“+selectionBound”的有效值);
userSelection=dataIn.nextInt;
}
返回用户选择;
}

您看到这些错误的原因是
dataIn
main
方法的本地方法,这意味着除非您明确地将扫描仪传递给该方法,否则其他方法无法访问它

有两种解决方法:

  • 将扫描仪传递到
    DataTest
    方法,或
  • 在课堂上使扫描器
    静止
以下是如何通过扫描仪:

public static int DataTest(int selectionBound, Scanner dataIn) ...
下面是如何使
扫描仪
静态:替换

Scanner dataIn = new Scanner(System.in);
main()中使用

static Scanner dataIn = new Scanner(System.in);

main
方法之外。

您不能访问在其他方法中声明的变量,即使是main方法。这些变量具有方法作用域,这意味着它们不存在于声明它们的方法之外。您可以通过将Scanner声明移到所有方法之外来解决此问题。这样,它将享受类的范围,并且可以在主类中的任何地方使用

class Main{

     //this will be accessable from any point within class Main
     private static Scanner dataIn = new Scanner(System.in);

     public static void main(String[] args){ /*stuff*/ }

     /*Other methods*/
}
作为java中的一般经验法则,变量不存在于声明它们的最小的
{}
对之外(唯一的例外是定义类体的
{}
):


“使扫描器在类中是静态的”我想你的意思是“使扫描器成为类中的静态属性”。@Pescis我用示例扩展了答案。这实际上也解决了我在几种方法中使用的随机生成器的一个问题
void someMethod() 
{ 
      int i; //i does not exist outside of someMethod
      i = 122;
      while (i > 0)
      {
           char c; //c does not exist outside of the while loop
           c = (char) i - 48;
           if (c > 'A')
           {
                boolean upperCase = c > 90; //b does not exist outside of the if statement
           }
      }



      {
          short s; //s does not exist outside of this random pair of braces
      }
}