public static void main(String[] args) { int i = 0; i = rollDice(11); System.out.print(" \nThe number of rolls it took: " + i); i = rollDice(5); System.out.print(" \nThe number of rolls it took: " + i); } public static int rollDice(int desiredNum) { int dice1 = 0; int dice2 = 0; Random rand = new Random(); int sum = 0; int count = 0; do { dice1 = rand.nextInt(6) +1; dice2 = rand.nextInt(6) +1; sum = dice1 + dice2; count++; } while(sum != desiredNum); return count; } }
Im wanting to make it where the user can enter their own desired sum of the numbers to be rolled .Also I'm wanting it to display the value of each rolled die as its rolled. It needs to allow the user to call the rollDice method as many times as they want to.
Heres my exmaple output
EX- Please enter the Desired number: 8 Roll 1: 4 6 Sum: 10 Roll 2: 3 5 Sum: 8 It took 2 rolls to get the Desired number.
The original code above WAS a lab i had to do a few weeks ago. But we have just started this. And im trying to get ahead of the class. And this community helps alot. Thanks in advance.