2016-04-06 9 views
0

Bu programla ilgili sorun yaşıyorum. Özel yöntemdeki her şeyin doğru olduğuna inanıyorum, ancak, döngü oluşturma ana yöntemini alamıyorum. Kod aşağıda ve yorumlarda yaptığı şeyin bir açıklamasıdır.Java'da Döngü ....?

Teşekkürler.

import java.util.Random; 

/** 
* This program rolls the dice three times and the profit is the sum of the face 
* values that are obtained. Fives do not count and no roll after a five counts. 
* The program will be run 1 million times and the results will be averaged 
* 
* @author -- 
*/ 
public class FatalFives { 

    public static void main(String[] args) { 
     final int TRIALS = 1000000; 
     int count = 0; 

     while (count < TRIALS) { 
      count++; 
     } 

     double avg = (double) profit/TRIALS; 
     System.out.println("Your average winnings are: $" + avg); 
    } 

    private static int FatalFive() { 
     Random rand = new Random(); 

     int profit = 0; 

     int die1 = 1 + rand.nextInt(6); 
     int die2 = 1 + rand.nextInt(6); 
     int die3 = 1 + rand.nextInt(6); 

     if (die1 == 5) { 
      //System.out.println("You're unlucky, no profit"); 
      profit = 0; 
     } else if (die1 != 5 && die2 == 5) { 
      //System.out.println("Congrats! You won $" + die1); 
      profit = (die1); 
     } else if (die3 == 5) { 
      //System.out.println("Congrats! You won $" + die1 + die2); 
      profit = (die1 + die2); 
     } else if (die1 != 5 && die2 != 5 && die3 != 5) { 
      //System.out.println("Congrats! You won $" + (die1 + die2 + die3)); 
      profit = (die1 + die2 + die3); 
     } 
     return profit; 
    } 
} 

cevap

0
  • Rastgele başlatılması gerektiği bir kez. Her seferinde
  • aynı sonuçları oluşturabilir, özel yöntem döngüde hiçbir zaman çağrılmaz. Niye ya?
  • kâr özel yöntemle döndürülen kar saklamak için posta yönteminde tanımlı değil

değiştirebilir olmalıdır:

while (count < TRIALS) { 
     count++; 
    } 

gibi bir şey için:

long profit = 0; 
    while (count < TRIALS) { 
     profit += FatalFive(); 
     count++; 
    } 

Lütfen Not: Kurucu dışında bir yöntem, sınıf gibi adlandırılmamalıdır. yönteminizi "rollFatalFive()" gibi bir şeye yeniden adlandırın.