java - Lucky Sevens [Net Change Calculation] -


whenever execute program average change 0 , not understand why.

/*luckysevens.java  simulate game of lucky sevens until funds depleted. 1) rules:         roll 2 dice         if sum equals 7, win $4, else lose $1 2) inputs are:         amount of money user prepared lose 3) computations:         use random number generator simulate rolling dice         loop until funds depleted         count number of rolls         keep track of maximum amount 4) outputs are:         number of rolls takes deplete funds         maximum amount         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,       // 2 dice           dollars,          // initial number of dollars (input)           countatmax,       // count when maximum achieved           count,            // number of rolls reach depletion           maxdollars,       // maximum amount held gambler           averagewin,       // average net change after 100 rolls           initialdollars;   // initial amount of money user has            // request input       system.out.print("how many dollars have? ");       dollars = reader.nextint();        // initialize variables       maxdollars = dollars;       initialdollars = dollars;       countatmax = 0;       count = 0;        // loop until money gone       while (dollars > 0){          count++;           // roll dice.          die1 = generator.nextint (6) + 1; // 1-6          die2 = generator.nextint (6) + 1; // 1-6           // calculate winnings or losses          if (die1 + die2 == 7)             dollars += 4;          else             dollars -= 1;            // if new maximum, remember          if (dollars > maxdollars){             maxdollars = dollars;             countatmax = count;          }           /* todo:fix below statement                  returns influx 0 */           if (count == 100) {            averagewin = ((maxdollars - initialdollars) / 100);             system.out.println ("in first 100 rolls there average money influx of " + averagewin + " per roll.") ;           }       }        // display results       system.out.println          ("you went broke after " + count + " rolls.\n" +           "you should have quit after " + countatmax +           " rolls when had $" + maxdollars + ".");    } } 

make averagewin double variable.

change calculation of average win below

averagewin = ((double)maxdollars - (double)initialdollars) / 100;


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -