Reputation: 19
I have an assignment to create a program that rolls 2 6-sided dice. Every-time you roll the dice, it costs you $1. However, when you roll a 7, you gain $4. This loops until you are out of money. I have all this down, but there is a tricky part of the assignment I can't figure out. You have too track the highest amount of money you had during how many times you rolled, and how many rolls it was that you had that highest amount of money. An example of how this looks would be like
You should have stopped playing after 43 rolls when you had 127$.
I can not figure out how too track the highest amount of money earned during the game. Here is my code, thank you for any help.
package dicegamee;
import java.util.Random;
import java.util.Scanner;
public class Dicegamee {
public static void main(String[] args) {
int diceone = 0; // declaring dice one as an int
int dicetwo = 0; // declaring dice two as an int
int money = 0; // declaring money as an int
int count = 0;
Random rand = new Random(); // setting random
Scanner reader = new Scanner(System.in); // setting scanner
System.out.println( "Hello, welcome to the dice game, one dollar is one game, how many are you willing to play? : "); // opening message
money = reader.nextInt(); // setting money too what you enter
while (money > 0) {
diceone = rand.nextInt(6); //rolling dice one
dicetwo = rand.nextInt(6); //rolling dice two
count++;
if (diceone + dicetwo == 7) { // if dice one and dice two equal seven statemen
money += 4;
}
}
money--; //subtracting one from money entered
}
System.out.println("It took " + count + " rolls too lose all your money"); // how many times the dice equaled seven printed
Upvotes: 1
Views: 96
Reputation: 2690
You'll need to keep track of both throughout the game. If you have more money than you have had before, note that amount and the number of rolls it has taken you to get there. When the game ends, you can print both values.
Upvotes: 1