我无法从另一个类访问我的变量

时间:2015-01-09 14:36:49

标签: java

我正在研究一个随机数字的简单宾果游戏。我的主要课程(Bingo)在这里:(注意我有更多的代码与问题无关)`

public class Bingo {
    public static void main (String[] args) {
        RandNum columnB = new RandNum();
        columnB.max = 15;
        columnB.randNum();
        int b1 = columnB.row1;
        int b2 = columnB.row2;
        int b3 = columnB.row3;
        int b4 = columnB.row4;
        int b5 = columnB.row5;

        RandNum columnI = new RandNum();
        columnI.min = 16;
        columnI.max = 30;
        columnI.randNum();
        int i1 = columnI.row1;
        int i2 = columnI.row2;
        int i3 = columnI.row3;
        int i4 = columnI.row4;
        int i5 = columnI.row5;

        RandNum columnN = new RandNum();
        columnN.min = 31;
        columnN.max = 45;
        columnN.randNum();
        int n1 = columnN.row1;
        int n2 = columnN.row2;
        int n3 = columnN.row3;
        int n4 = columnN.row4;
        int n5 = columnN.row5;

        RandNum columnG = new RandNum();
        columnG.min = 46;
        columnG.max = 60;
        columnG.randNum();
        int g1 = columnG.row1;
        int g2 = columnG.row2;
        int g3 = columnG.row3;
        int g4 = columnG.row4;
        int g5 = columnG.row5;

        RandNum columnO = new RandNum();
        columnO.min = 61;
        columnO.max = 75;
        columnO.randNum();
        int o1 = columnO.row1;
        int o2 = columnO.row2;
        int o3 = columnO.row3;
        int o4 = columnO.row4;
        int o5 = columnO.row5;
    }
}

我用来生成随机数的课程:

public class RandNum {
    int min = 1;
    int max;

    public void randNum() {
        Random rn = new Random();

        int row1 = rn.nextInt(max - min + 1) + min;
        int row2 = rn.nextInt(max - min + 1) + min;
        int row3 = rn.nextInt(max - min + 1) + min;
        int row4 = rn.nextInt(max - min + 1) + min;
        int row5 = rn.nextInt(max - min + 1) + min;
    }
}

现在的问题是我试图将我的b1,b2等变量设置为RandNum类中的变量row1等。我知道我可以把它全部放在一个班级,但这是我多次遇到的问题,所以我想解决它。非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

您的b等变量的范围限定为main的{​​{1}}方法,因此您永远无法从其他类访问它们 - 或者同一类的其他方法那件事。

您可以将它们设置为Bingo的{​​{1}}字段,而不是在方法体内(或带有getter的public static)声明它们,但由于此处有变量的数量,我建议先重构并调查一些数据结构,例如Bingoprivate

答案 1 :(得分:1)

您在rowX方法中将randNum()变量声明为局部变量会发生什么。

这使得它的范围只是那种方法,所以它们不会存在/在它之外可用。

你能做什么? 将它们声明为类属性:

public class RandNum {
    int row1, row2, row3, row4, row5;
    ...
    public void randNum() {...}
}

注意:您可以改为使用数组:

int[] rows = new int[5];

然后在你的方法中:

public void randNum() {
    Random rn = new Random();

    for (int i = 0; i < rows.length(); i++)
        rows[i] = rn.nextInt(max - min + 1) + min;
}

答案 2 :(得分:0)

这应该解决它。变量b1b2,...是randNum函数的本地变量,一旦执行退出函数,就会超出范围。解决方案是将变量存储在RandNum类的字段中。

public class RandNum{
    private int[] rows = new int[5];
    private int min;
    private int max;

    public RandNum(int min, int max) {
        Random rn = new Random();
        for(int i=0;i<5;++i)
           rows[i] = rn.nextInt(max - min + 1) + min;
    }

    public int getRow(int row) {
       return rows[i];
    }
}

public static void main (String[] args) {
    RandNum columnB = new RandNum(1,15);
    int b1 = columnB.getRow(0);
    int b2 = columnB.getRow(1);
    int b3 = columnB.getRow(2);
    int b4 = columnB.getRow(3);
    int b5 = columnB.getRow(4);

    ...
}

答案 3 :(得分:0)

请参阅: Why use getters and setters?

创建访问者/变更者:

public class RandNum {
    private int min;
    private int max;

    public RandNum(int min, int max) {
        setMin(min);
        setMax(max);
    }

    public RandNum() {
        this(1, 0);
    }

    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public void setMax(int max) {
        this.max = max;
    }

    // ...
}

然后使用它们:

public class Bingo {
    public static void main (String[] args) {
        RandNum columnB = new RandNum();
        columnB.setMax(15);
        // ...
    }
}