go to previous page   go to home page   go to next page hear noise highlighting

Answer:

The variable endScore is initialized to 10, and since it is declared final is not allowed to change.


The Tosses

The computer's toss is straightforward. This is just summing up the toss of two individual six-sided dice.


// Computer's Toss
compToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ;
System.out.println("The Computer tosses: " + compToss);

For the user's toss, first the program asks the user which dice to use:


// Player's Toss
System.out.print("Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? ");
String numDice = scan.nextLine();

No numerical use is made of the digit that the user types, so it is best to read it in as a string. That way if the user accidentally hits a non-digit the program will not crash. String comparison is then used to determine which option to take. Everything but digit "1" will result in the two-dice branch.


if ( numDice.equals("1") )
{
  playerToss = rand.nextInt(11)+2 ;
  System.out.println("You throw 1 die and get: " + playerToss );
}
else
{
  playerToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ;
  System.out.println("You throw 2 dice and get: " + playerToss );
}

QUESTION 21:

Wouldn't it be better to test if the user types exactly the specified choices?


go to previous page   go to home page   go to next page