JAVA - 无法调用我的数组方法

JAVA - can't call my array method

Eclipse 说:'chiffres cannot be resolved to a variable',如何修复调用方法?

public class Table {

public static void main(String[] args) {


    Tableau1 table = new Tableau1();


    table.CreerTable();
    table.AfficherTable(chiffres);

}}

部分: class Tableau1 with array: 声明它

public class Tableau1 {
int [][] chiffres;
int nombre;
public void CreerTable(){

    int[][] chiffres= {{11,01,3},
                        {12,02,4},
                        {12,03,5}};
    //edited
    this.chiffres=chiffres;


}

public int[][] AfficherTable(int[][] chiffres){
    this.nombre=12;
    for(int i=0;i<2;i++){

    System.out.println("essai"+chiffres[i][1]);
    if(chiffres[i][0]==nombre){System.out.println("ma ligne ="+chiffres[i][0]+","+chiffres[i][1]+","+chiffres[i][2]);
                                };

                        }
                        return chiffres;
}

}

非常感谢

你这里有3个问题。

问题 1 :

1) 您的方法 AfficherTable(chiffres) 不需要传递参数,因为它是一个实例成员。

您可以直接调用

table.AfficherTable();

这解决了你的问题。

在做第 2 题之前

问题二:

2) 您已将 chifferes 声明为实例成员 int [][] chiffres;

你正在构造函数中初始化它

public void CreerTable(){

    int[][] chiffres= {{11,01,3},
                        {12,02,4},
                        {12,03,5}};

}

但是如果你仔细观察,你又在创建新的数组。那是行不通的,因为您正在创建新数组并忘记了您的实例成员。

将构造函数更改为

public void CreerTable(){

        chiffres= new  int[3][3] {{11,01,3},
                            {12,02,4},
                            {12,03,5}};

    }

问题 3 :

更改该构造函数后,由于您在同一个 class 成员中使用它,因此不需要接收它。因此,您将方法声明更改为

public int[][] AfficherTable(){

我想你现在会没事的。

    table.CreerTable();
    table.AfficherTable(chiffres);

通过解析 chiffres,它会在 Class Table 中搜索,因为您没有指定 chiffres 来自 Tableau1。 因此解决方案是:

    table.CreerTable();
    table.AfficherTable(table.chiffres);

chiffers既不是main方法的局部变量,也不是classTable的字段,所以报错。