2015-01-09 54 views
-2

我正在研究一個簡單的賓果遊戲,它有隨機數字。我的主類(賓果)是在這裏:(注意,我有更多的代碼是不相關的問題)`我無法從另一個班級訪問我的變量

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

現在的問題是,我試圖在RandNum類中設置我的b1,b2等變量到變量row1等。我知道我可以把它全部放在一個班,但這是我多次遇到的問題,所以我想解決它。任何幫助深表感謝。

+1

請使用一個循環? –

回答

1

發生了什麼是您在randNum()方法中聲明rowX變量爲局部變量。

這使得它的範圍只有這種方法,所以它們不會存在/在外面可用。

你能做什麼? 聲明爲類屬性:

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

你的b等變量的作用域爲main方法Bingo所以你將永遠無法從另一個類或同一類的另一個方法訪問它們。

你可以將它們設置爲Bingopublic static領域,而不是方法的身體(或private s的干將)內聲明它們,但由於你在這裏的變量的數目,我首先建議重構和調查一些數據結構如Collection s和Map s。

0

這應該解決這個問題。變量b1,b2,...是您的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); 

    ... 
} 
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); 
     // ... 
    } 
} 
相關問題