How do I fix the error where I cannot make a static reference to a non-static input field?

后端 未结 5 660
刺人心
刺人心 2021-01-26 22:38

I am learning java. I wrote the following code but I am getting this error \"cant make a static reference to a non static input field\" in Arrayfunction(), when I try to take in

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-26 23:20

    either make your Scanner static and use it inside the static methods or create an instance of the class an access scanner from your static method.

    static Scanner input= new Scanner(System.in);
    public static void Arrayfunction(int array[][])
    {          
                System.out.println("Enter a number");
                array[i][j]=input.nextInt();// error
     }
    

    OR

    Scanner input= new Scanner(System.in);
    public static void Arrayfunction(int array[][])
    {
                System.out.println("Enter a number");
                array[i][j]=new MultidimArrays().input.nextInt();// error
          }
    

提交回复
热议问题