2016-04-03 18 views
-2

Yazdığım bu programla ilgili yardım almak istiyorum. İki tane de, her ikisi de birlikte eklendikleri yüz frekanslarıyla çıkış yapmak istiyorum. İşte yazdığım mevcut kod. Şimdi çok basit ve çok zayıf yazılmış. Java'da kod yazmak için çok yeniyim, lütfen bana yardımcı olacak herhangi bir ipucu ve püf noktası verin :).İki zar nasıl verilir ve birlikte eklenir

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

Soru nedir? Çalışmayan bir şey var mı? Neden "java.lang.reflect.Array" dosyasını içe aktarıyorsunuz? – Thilo

+1

'dice2' hiç kullanılmamış, bu kasıtlı mı? Daha spesifik bir soru sormalısınız. –

cevap

-1

Aşağıdaki kod her yüz haddelenmiş kaç kez basar. Rastgele java kütüphanesi tarafından kullanılan rasgele sayı algoritması nedeniyle tüm yüzlerin neredeyse aynı zaman zarfında döndüğünü göreceksiniz. Herhangi bir sorunuz varsa sorunuz!

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

Teşekkür ederiz! Şimdi biraz daha iyi bir anlayışa sahibim. – Nomide

İlgili konular