I'm working on building out a sample dice rolling game that rolls the dice 10 times unless interrupted by user input.
I have a while statement that runs based on the state of two boolean values, one of which is set based on the user input.
My current progress is below. Any suggestions would be a huge help!
import java.util.Scanner;
import java.swing.JOptionPane;
import java.util.Random;
public class DiceGame
{
public static void main(String[] args)
{
Random rand = new Random();
int player1Wins = 0;
int player2Wins = 0;
int ties = 0;
int rollCount = 0;
boolean rollAgain = true;
while (rollCount < 10 && rollAgain)
{
int player1Dice = rand.nextInt(6) + 1;
int player2Dice = rand.nextInt(6) + 1;
if (player1Dice > player2Dice)
{
player1Wins++;
System.out.println("Player 1 wins!!");
}
else if (player2Dice > player1Dice)
{
player2Wins++;
System.out.println("Player 2 wins!!");
}
else
{
ties++;
System.out.println("It's a tie...");
}
rollCount++;
String answer;
Scanner keyboard = new Scanner(System.in);
System.out.println("Would you like to roll again? Press y for yes");
answer = keyboard.nextLine();
if (answer == "y")
{
rollAgain = true;
}
else
{
rollAgain = false;
}
}
System.out.println();
System.out.println("Player 1 Total wins: " + player1Wins);
System.out.println("Player 2 Total wins: " + player2Wins);
System.out.println("Total Ties: " + ties);
System.out.close();
}
}