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

chr*_*_cx 1 java arrays methods call

Eclipse说:'chiffres无法解析为变量',如何修复调用方法?

public class Table {

public static void main(String[] args) {


    Tableau1 table = new Tableau1();


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

}}
Run Code Online (Sandbox Code Playgroud)

part:和类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;
}
Run Code Online (Sandbox Code Playgroud)

}

非常感谢

Sur*_*tta 6

你这里有3个问题.

问题1:

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

你可以简单地打电话

table.AfficherTable();
Run Code Online (Sandbox Code Playgroud)

这解决了你的问题.

在做这个问题之前没有2

问题2:

2)您chifferes作为实例成员进行了delcaredint [][] chiffres;

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

public void CreerTable(){

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

}
Run Code Online (Sandbox Code Playgroud)

但如果你仔细看,你又在创造新阵列.这不起作用,因为您正在创建新数组并忘记您的实例成员.

将构造函数更改为

public void CreerTable(){

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

    }
Run Code Online (Sandbox Code Playgroud)

问题3:

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

public int[][] AfficherTable(){
Run Code Online (Sandbox Code Playgroud)

我猜你现在好了.