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

标签 java arrays methods call

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

public class Table {

public static void main(String[] args) {


    Tableau1 table = new Tableau1();


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

}}

部分: 和类 Tableau1 数组:声明它

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:

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:

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

public int[][] AfficherTable(){

我想你现在会没事的。

关于JAVA - 无法调用我的数组方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646887/

相关文章:

java - 尝试使用 jtextfield 捕获异常?

c++ - 数组中的订单号

arrays - 如何以 3D 表示形式有效存储大量但极其稀疏的数据?

php - 如何给 PHP 中的函数起有意义的名称?

Python - 从其他类中的方法检索值

java - 使用构建器模式进行 Spring 依赖注入(inject)

java改变hashmap的值

java - 如何在java中的超时值后停止/杀死多个线程

Java 为 ComboBox 赋值

java - 编译时音频剪辑不播放