I have been helped here once before. I got the info I needed to solve my issues. I'm a beginner and this online class I'm taking is killing me. This assignment involves the use of loops, most likely do-while, and possibly others. Essentially the program is to have the user guess what the total of his roll of two dice will be (2 to 12). Then the two dice are rolled. The user has three rolls to achieve a WIN, if not he loses and must get the opportunity to play again. If he matches his guess, the game tells him he's a winner and the game is over then - no three rolls. He also must be given the opportunity to play again.
I've validated the user's guess (input). That part works great. This program uses a class that rolls and displays the dice - that too works ok. From there, I've gotten completely confused and frustrated. Here is my code, most of which does not work. Can anyone help me? I'm such a beginner, the book doesn't always help, and there is no online course assistance. Thanks in advance for any hints or help:
//********************************************************************
// DiceGame.java
// This program tells user to predict what the pair of dice will roll.
// Dice will roll a maximum of 3 times, using the Dice class already created.
// If the dice rolls the user's prediction in the 3 rolls, user wins
// If not, user loses. Either way user has option to play again.
//********************************************************************
import java.util.Scanner;
public class DiceGame
{
public static void main (String[] args)
{
// All variables including char variable that holds y and n.
int rolls, roll1, roll2, guessRoll;
int rollSum;
char repeat = ' ';
String input;
Scanner keyboard = new Scanner(System.in);
// Create two separate dice objects to create a pair of dice
Dice die1 = new Dice();
Dice die2 = new Dice();
// get the user to enter the what user predicts his/her pair of dice will roll
System.out.print ("Enter what your pair of dice will roll (a number 2 through twelve please: ");
guessRoll = keyboard.nextInt();
// Validate user's input - page 261
while (guessRoll < 2 || guessRoll > 12)
{
System.out.println("Incorrect number entered. Please enter a number from 2 to to 12 only.");
System.out.println("Enter correct number here: ");
guessRoll = keyboard.nextInt();
}
{
do
{
roll1 = die1.diceRoll();
roll2 = die2.diceRoll();
rollSum = roll1 + roll2;
// Set the number of times the loop can repeat itself.
for (rolls = 1; rolls < 3; rolls++)
{
System.out.println("Your Dice rolled: " + rollSum +".");
System.out.println("NO MATCH");
}
while (repeat == 'Y' || repeat == 'y')
{ System.out.println("Play again? Enter Y or N: ");
input = keyboard.next();
repeat = input.charAt(0);
}
} while (guessRoll != rollSum);
}
do
{
//This will run if the correct number is guessed
System.out.println("_______________Roll #: " + rolls + " _________________");
roll1 = die1.diceRoll();
roll2 = die2.diceRoll();
rollSum = roll1 + roll2;
System.out.println("Your Dice rolled: " + rollSum +".");
System.out.println("IT'S a MATCH - YOU WIN!!");
while (repeat == 'Y' || repeat == 'y')
{
System.out.println("Play again? Enter Y or N: ");
input = keyboard.next();
repeat = input.charAt(0);
}
} while (guessRoll == rollSum);
}
}