2015-01-09 3 views
-2

숫자가 무작위 인 간단한 빙고 게임을 진행하고 있습니다. 내 주요 클래스 (빙고) 여기에 있습니다 :다른 클래스의 변수에 액세스 할 수 없습니다.

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

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; 
    } 
} 
내가 난수를 생성하는 데 사용하고

내 클래스 (I 문제에 관련이없는 더 많은 코드가 있습니다)

이제 문제는 b1, b2 등 변수를 RandNum 클래스의 변수 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

main 등의 변수 범위는 Bingo이며 다른 클래스 또는 동일한 클래스의 다른 메소드에서 액세스 할 수 없습니다

대신 메소드의 몸 (또는 게터와 private들) 내에서 선언의 Bingopublic static 필드로 설정할 수 있지만, 때문에 당신이 여기있는 변수의 수에, 내가 먼저 리팩토링을 제안하고 몇 가지 데이터 구조를 조사 예 : CollectionMap입니다.

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); 
     // ... 
    } 
} 
관련 문제