我如何修正这个错误,我不能让一个静态引用的非静态输入字段? [英] How do I fix the error where I cannot make a static reference to a non-static input field?

查看:212
本文介绍了我如何修正这个错误,我不能让一个静态引用的非静态输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习java。我写了下面code,但我得到这个错误不能让一个静态引用到一个非静态的输入域中Arrayfunction(),当我试图把输入。为什么会这样,我该如何解决这个问题?

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 input. Why is this so and How can I fix it?

import java.util.*;
public class MultidimArrays {

Scanner input= new Scanner(System.in);

public static void main(String args[])
{       
    int array[][]= new int[2][3];

    System.out.println("Passing array to a function");

    Arrayfunction(array);
}

public static void Arrayfunction(int array[][])
{
    System.out.println("Inside Array function");

    for(int i=0;i<array.length;i++)
    {
        for(int j=0;j<array[i].length;j++)
        {
            System.out.println("Enter a number");
            array[i][j]=input.nextInt();// error
    }
}

}

推荐答案

扫描仪没有被定义为静态的,因此是在错误的范围

Scanner is not defined as static therefore is in the wrong scope

无论是创建在 Arrayfunction 扫描仪实例或以

Either create the Scanner instance inside Arrayfunction or create your scanner with

private static Scanner input= new Scanner(System.in);

这篇关于我如何修正这个错误,我不能让一个静态引用的非静态输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆