This program simulates rolling dice to win or lose. It tracks the number of wins and losses over multiple rounds. In each round, two dice are randomly rolled and their values printed. If the dice match, the player wins; otherwise, they lose. After all rounds, a report is printed with the total games played and wins/losses. The user is prompted after each round to play again.
Java programI made this Account.java below. Using the attached cod.pdf
Simulate dice rolling and odds
1. /************************************************************************************
**********
* Amy Baxter
* Program 07
*
* This program is designed to simulate a dice rolling/odds program.
*************************************************************************************
********/
import java.util.*;
public class Lab07 {
public static void main(String[] args) {
// Declare Variables
int loses = 0;// keeps track of number of loses
int wins = 0;// keeps track of wins
int dice1;
int dice2;
boolean playAgain = true;
welcome();
while (playAgain == true) {
dice1 = rollDie();
dice2 = rollDie();
printDice(dice1, dice2);
if (isPair(dice1, dice2)) {
wins++;
System.out.println("Congrats! You won!");
} else {
loses++;
System.out
.println("You lost, you're really bad at this
game...");
}
playAgain = playAgain();
}
report(wins, loses);
}
// displays a brief accounting of the rules and format of the game
public static void welcome() {
System.out.println(" Welcome to Computer Dice 2013n"
2. + "-------------------------------------n"
+ "You will first roll your dicen"
+ "Next the outcome of your roll willn" + "be
determined:n"
+ "Any pair and you Winn" + "Anything else and you
Losen"
+ "--------------------------------------n");
}
// returns an int in the range [1, 6]
public static int rollDie() {
Random generator = new Random();
int roll = generator.nextInt(6) + 1;
return roll;
}
// formats d1 and d2 as a roll of two die and prints to the screen
public static void printDice(int d1, int d2) {
System.out.println(" Player n--------n " + d1 + " " + d2);
}
// returns true only when d1 == d2
public static boolean isPair(int d1, int d2) {
if (d1 == d2) {
return true;
} else {
return false;
}
}
// displays a report of the game's outcomes to the screen
public static void report(int wins, int loses)
{
System.out.println("Computer Dice Resultsn"
+ "---------------------n" + "You played " + (wins +
loses)
+ " roundsn" + "Rounds won : " + wins + "n"
+ "Rounds lost : " + loses + "n" + "---------------------
");
}
// ask the user if they want to play again.
public static boolean playAgain() {
Scanner scanner = new Scanner(System.in);
boolean valid = false;
String input = "f";
3. while (valid == false) {
System.out.println("Do you want to play again [y,n]:");
input = scanner.next();
//input = input.toLowerCase();
if (input.equals("y") || input.equals("n")) {
valid = true;
}
}//end while
//scanner.close();
// Return true if y else return false
if (input.equals("y")) {
return true;
} else {
return false;
}
}
}