2015-04-29 38 views
2

每當我執行程序時,平均變化總是0,我不明白爲什麼。Lucky Sevens [淨變化計算]

/*LuckySevens.java 

Simulate the game of lucky sevens until all funds are depleted. 
1) Rules: 
     roll two dice 
     if the sum equals 7, win $4, else lose $1 
2) The inputs are: 
     the amount of money the user is prepared to lose 
3) Computations: 
     use the random number generator to simulate rolling the dice 
     loop until the funds are depleted 
     count the number of rolls 
     keep track of the maximum amount 
4) The outputs are: 
     the number of rolls it takes to deplete the funds 
     the maximum amount 
     the average net change after 100 rolls 
*/ 

import java.util.Scanner; 
import java.util.Random; 

public class LuckySevens { 
    public static void main (String [] args) { 

     Scanner reader = new Scanner(System.in); 
     Random generator = new Random(); 

     int die1, die2,  // two dice 
      dollars,   // initial number of dollars (input) 
      countAtMax,  // count when the maximum is achieved 
      count,   // number of rolls to reach depletion 
      maxDollars,  // maximum amount held by the gambler 
      averageWin,  // the average net change after 100 rolls 
      initialDollars; // initial amount of money user has 




     // Request the input 
     System.out.print("How many dollars do you have? "); 
     dollars = reader.nextInt(); 

     // Initialize variables 
     maxDollars = dollars; 
     initialDollars = dollars; 
     countAtMax = 0; 
     count = 0; 

     // Loop until the money is gone 
     while (dollars > 0){ 
     count++; 

     // Roll the dice. 
     die1 = generator.nextInt (6) + 1; // 1-6 
     die2 = generator.nextInt (6) + 1; // 1-6 

     // Calculate the winnings or losses 
     if (die1 + die2 == 7) 
      dollars += 4; 
     else 
      dollars -= 1; 


     // If this is a new maximum, remember it 
     if (dollars > maxDollars){ 
      maxDollars = dollars; 
      countAtMax = count; 
     } 

     /* TODO:FIX BELOW STATEMENT 
       it always returns influx as 0 */ 

     if (count == 100) { 
      averageWin = ((maxDollars - initialDollars)/100); 
      System.out.println ("In the first 100 rolls there an average money influx of " + averageWin + " per roll.") ; 

     } 
     } 

     // Display the results 
     System.out.println 
     ("You went broke after " + count + " rolls.\n" + 
      "You should have quit after " + countAtMax + 
      " rolls when you had $" + maxDollars + "."); 
    } 
} 
+0

您多久測試一次該程序?因爲很可能你會放鬆一些遊戲,然後再也不會回頭看看maxDollars。因此,您絕不會將maxDollars更改爲initialDollars。這給你0/100 - > 0 – ceekay

+0

可能的重複[在Java中總是結果爲零(0)?](http://stackoverflow.com/questions/10455677/division-in-java-always-results-in -zero-0) – timrau

+0

這個問題很可能是由於AverageWin是一個整數引起的。嘗試將它改爲浮點/雙精度,以防止它被降低到下一個較低的整數(在您的情況下可能爲0),並查看是否改變了結果。可能不是問題,但整數除法是第一件要吸引我的眼睛 – Sossenbinder

回答

3

製作平均值溫度作爲雙重變量。

平均贏的變化計算如下

averageWin =((雙)maxDollars - (雙)initialDollars)/ 100;

0

我試過以下來得到正確的答案。

更改類型averageWin

double averageWin; 

,然後計算公式

averageWin = (maxDollars - dollars)/100.0; 

這將保存精度和給你正確的答案。