简体   繁体   中英

JAVA - can't call my array method

Eclipse says: 'chiffres cannot be resolved to a variable', how to fix the call method ?

public class Table {

public static void main(String[] args) {


    Tableau1 table = new Tableau1();


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

}}

part: and class Tableau1 with array: to declare it

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;
}

}

Thanks a lot

You have 3 problems here.

Problem 1 :

1) You method AfficherTable(chiffres) need not to pass an argument, since it is an instance member.

You can simply call

table.AfficherTable();

That solves your problem.

Before doing that problem no 2

Problem 2:

2) You delcared chifferes as a instance member int [][] chiffres;

and you are initializing it's in constructor

public void CreerTable(){

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

}

But if you closely look, you are creating new array again. That won't work, since you are creating new array and forgot your instance member.

Change your constructor to

public void CreerTable(){

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

    }

Problem 3 :

After changing that constructor, since you are using it in the same class member, you need not to receive it. Hence you change your method declaration as

public int[][] AfficherTable(){

You'll be fine I guess now.

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

By resolving chiffres it searches in the Class Table as you don't specify that chiffres comes from Tableau1. Therefore the solution is:

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

chiffers既不是main方法的局部变量,也不是Table类的字段,这就是错误的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM