2016-04-03 18 views
-2

私が書いているこのプログラムの助けをしたいと思います。 2つの顔の周波数を合わせて出力したい。ここに私が書いた現在のコードがあります。それは今非常に基本的なものであり、非常によく書かれていません。私はJavaでコーディングするのがとても新しいので、私に役立つヒントやトリックを教えてください:)。2つのサイコロを出力して一緒に追加するには

import java.util.Random; 

public class DiceRolling { 

    public final static Random randomNumbers = new Random(); 

    public static void main(String[] args) { 

     int[] dice1 = new int[7]; 
     int[] dice2 = new int[7]; 

     // prints out the headings 

     for (int headOne = 1; headOne <= 6; headOne++) 
      System.out.print(" " + headOne); 

     System.out.println(); 

     for (int headTwo = 1; headTwo <= 6; headTwo++) 
      System.out.println(headTwo); 

     // The rolls of the two dices begins here 

     for (int frequencyOne = 0; frequencyOne < 36000000; frequencyOne++) { 
      ++dice1[1 + randomNumbers.nextInt(6)]; 
     } 

     // output faces of die 
     for (int faceOne = 1; faceOne < dice1.length; faceOne++) { 
      System.out.print(" " + dice1[faceOne]); 
     } 
    } 

} 
+3

問題は何ですか。動かないものがありますか?なぜあなたは 'java.lang.reflect.Array'をインポートしますか? – Thilo

+1

'dice2'は決して使用されません、それは意図的ですか?より具体的な質問をする必要があります。 –

答えて

-1

次のコードは、各顔が何回ロールアウトされるかを表示します。あなたは、ランダムなJavaライブラリによって使用される乱数アルゴリズムのために、すべての顔がほぼ同じ回数だけロールしていることに気づくでしょう。ご質問がありましたらお聞かせください!

import java.util.HashMap; 
import java.util.Map.Entry; 
import java.util.Random; 

public class DiceRolling { 

    public final static Random randomNumbers = new Random(); 

    public static void main(String[] args) { 
     // create a hashmap to hold the amount of times each number was rolled 
     HashMap<Integer, Integer> frequencies = new HashMap<Integer, Integer>(); 

     // roll the dice 360000 times 
     for (int rolls = 0; rolls < 360000; rolls++) { 
      // roll dice 
      int result = (randomNumbers.nextInt(6) + 1); 

      // if the result has already happened add it 
      if (frequencies.containsKey(result)) { 
       frequencies.put(result, (frequencies.get(result) + 1)); 
       continue; 
      } 
      // else add it and set its frequency to 1 
      frequencies.put(result, 1); 
     } 

     // print results 
     for (Entry<Integer, Integer> entry : frequencies.entrySet()) { 
      System.out.println("Face: " + entry.getKey() + ", Times rolled: " + entry.getValue()); 
     } 
    } 
} 
+0

ありがとう!今私は今、より良い理解を少し持っています。 – Nomide

関連する問題