【问题标题】:array in constructor keeps returning as null?构造函数中的数组一直返回为空?
【发布时间】:2022-11-12 07:53:25
【问题描述】:
public class sierpinski {


    public static void main(String[] args) {

        sierpinski s1 = new sierpinski(3);

        System.out.println(String.valueOf(s1.pascal));


    }

    int row;
    String LString;
    int[] pascal;
    char[] Larray;



    public static int fact( int n) {
        int solution = 1;
        if (n == 0) {
            solution= 1;
            return solution;

        }
        else {
            for (int i = 2; i <= n; i++) {
                solution = solution * i;
            }

        }
        return solution;
    }

    public static int ncr( int n , int r){
        int ncr1 = fact(n)/(fact(r) * fact(n-r));
        return ncr1;
    }




    sierpinski( int row){

        this.row = row;

        char[] Larray = new char[row+1];

        int[] pascal = new int[row+1];

        for(int i =0; i < row+1; i++){

            int a = ncr(row, i);

            pascal[i] = a;


        }
        String LString = String.valueOf(Larray);

    }


}

我试图执行此代码,但当我在构造函数之外声明它时,pascal 一直返回为 null; 我也试过这个...

public class sierpinski {


    public static void main(String[] args) {

        sierpinski s1 = new sierpinski(3);

        System.out.println(String.valueOf(s1.pascal));


    }

    int row;
    String LString;
    


    public static int fact( int n) {
        int solution = 1;
        if (n == 0) {
            solution= 1;
            return solution;

        }
        else {
            for (int i = 2; i <= n; i++) {
                solution = solution * i;
            }

        }
        return solution;
    }

    public static int ncr( int n , int r){
        int ncr1 = fact(n)/(fact(r) * fact(n-r));
        return ncr1;
    }




    sierpinski( int row){

        this.row = row;

        char[] Larray = new char[row+1];

        int[] pascal = new int[row+1];

        for(int i =0; i < row+1; i++){

            int a = ncr(row, i);

            pascal[i] = a;


        }
        String LString = String.valueOf(Larray);

    }


}

我得到这个错误

sierpinski.java:8: error: cannot find symbol
        System.out.println(String.valueOf(s1.pascal));
                                            ^
  symbol:   variable pascal
  location: variable s1 of type sierpinski
1 error
error: compilation failed

说它找不到符号,任何解决方案,有人知道如何解决这个问题吗?

谢谢

尝试在顶部声明变量,但我不知道如何让它工作,有什么想法吗?

【问题讨论】:

    标签: java arrays oop constructor


    【解决方案1】:

    构造函数本质上是方法。

    sierpinski(int row) { // this is a constructor
    
      this.row = row; // this sets a field.
      int[] pascal = new int[row+1]; // this creates a new local variable
    
      // constructor ends, and with it, all local variables GO AWAY
    }
    

    你想声明一个字段。您已经有一个 (row),并且您已经设置了它 (this.row = row)。如果您希望 pascal 在构造函数结束时仍然存在,它需要是一个字段而不是本地:

    public Sierpinski {
      private int row;
      private int[] pascal;
    
      public Sierpinski(int row) {
        this.row = row;
        this.pascal = new int[row];
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2018-04-11
      • 2021-01-08
      • 2013-10-03
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多