2014-10-30 5 views
0

두 주사위 입력을 100000 번 굴려서 히스토그램으로 저장 한 간단한 프로그램을 작성해야합니다. 그러나, 나는 하나의 클래스 파일을 사용하여 모든 것을했습니다. 강사는 제가 메인을 사용하여 주사위를 관리하기를 원했지만 주사위 만 마쳤으나 주사위를 메인에 통합하는 방법을 모릅니다.메인을 사용하여 다이 관리

내가 쓴 프로그램 :

public class Histogram { 

public static void main(String[] args) { 

    int[] frequency = new int [13]; 
    int die1, die2; 
    int rolls; 
    int asterisk; 
    int total; 
    double probability; 

    rolls = 100000; 

    //Roll the dice 
    for (int i=0; i<rolls; i++) { 
     die1 = (int)(Math.random()*6) + 1; 
     die2 = (int)(Math.random()*6) + 1; 
     total = die1 + die2; 
      frequency[total]++;  
    } 

    System.out.println("Results" + '\n' + "Each " + '\"' + "*" + '\"' + " represents the probability in one percent."); 
    System.out.println("The total number of rolls is one hundred thousand."); 
    System.out.println("Value\tFrequency\tProbability"); 

    for (total=2; total<frequency.length; total++){ 
     System.out.print(total + ": \t"+frequency[total]+"\t\t"); 
     probability = (float) frequency[total]/rolls; 
     asterisk = (int) Math.round(probability * 100); 

     for (int i=0; i<asterisk; i++){ 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
} 

}

주사위 :

public class Dice { 

private int die1; 
private int die2; 

public Dice() { 

    roll(); 
    } 
public void roll() { 

    die1 = (int)(Math.random()*6) + 1; 
    die2 = (int)(Math.random()*6) + 1; 
    } 

public int getDie1() { 
    return die1; 
    } 
public int getDie2() { 
    return die2; 
    } 
public int getTotal() { 
    return die1 + die2; 
    } 
} 

답변

1

이 교체 :이

//Roll the dice 
for (int i=0; i<rolls; i++) { 
    die1 = (int)(Math.random()*6) + 1; 
    die2 = (int)(Math.random()*6) + 1; 
    total = die1 + die2; 
     frequency[total]++;  
} 

:

,
Dice d = new Dice(); 
for (int i = 0; i < rolls; i++) { 
    d.roll(); 
    frequency[d.getTotal()]++; 
} 

당신이 한 켤레의 주사위를 얼마나 잘 구현했는지 모르겠다. 나는 당신이 1에서 7까지 어느 곳 으로든 굴릴 수 있다고 생각합니다. 또한 "Bravo()"기능이 무엇인지 확실하지 않습니다. 아마도 제거 할 수 있습니다.

0

당신은 이런 식으로 뭔가를해야합니다 :

//Roll the dice 
Dice myDice = new Dice(); 
for (int i=0; i<rolls; i++) { 
    myDice.Bravo(); 
    die1 = myDice.getDie1(); 
    die2 = myDice.getDie2(); 
    total = myDice.getTotal(); 
    frequency[total]++;  
}