java - Loop with Dice rolling program, previous roll and double check -


a trivial problem sure can't quite work out how i'm meant previous dice integer remain same previous roll of die in program. think code self explanatory , such trivial program i'm kicking myself not being able head around it.

import java.util.random;  public class dice {      public static void main(string[] args) {         random rand = new random();         int min = 1;         int max = 6;         int loop = 0;         int dicerollone = 0;         int dicerolltwo = 0;         int dicetotal = 0;         int prevdicetotal = 0;          while (loop < 15000) {             loop++;             dicerollone = rand.nextint(max - min + 1) + min;             dicerolltwo = rand.nextint(max - min + 1) + min;             dicetotal = dicerollone + dicerolltwo;              system.out.println("dice roll 1: " + dicerollone);             system.out.println("dice roll 2: " + dicerolltwo);             system.out.println("dice total: " + dicetotal);             system.out.println("previous total: " + prevdicetotal);              prevdicetotal = dicetotal;              if (dicerollone == dicerolltwo || dicetotal == prevdicetotal) {                 system.out.println("after " + loop + " loops the");                 system.out.println("numbers match, nothing, lose, day sir!");                 system.exit(0);             }         }     } } 

the basic idea being 15,000 simulations. roll 2 dice. if roll double quit. if roll same sum in current roll sum of previous roll quit. i've tried debugging printing out previous dice total defaults 0 every time.

you want move prevdicetotal = dicetotal; after if statement.

        if (dicerollone == dicerolltwo || dicetotal == prevdicetotal) {             system.out.println("after " + loop + " loops the");             system.out.println("numbers match, nothing, lose, day sir!");             system.exit(0);         }          prevdicetotal = dicetotal; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -