Complete Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Just one, as usual. The one random number generator is used to simulate a pair of dice or one eleven-sided die, as needed.

Complete Program

Here is the complete program. Its sections will be expained shortly. You can copy and paste this program to a file and play with it, if you want.

import java.util.*;

class DiceChoiceGame
{
  public static void main ( String[] args )
  {
    // Declare and Initialize
    final int endScore=10;
    Scanner scan = new Scanner( System.in );
    Random rand = new Random();
    int playerScore=0, compScore=0;
    int playerToss, compToss;
    
    // Play Rounds until one player reaches ending score
    while ( playerScore<endScore && compScore<endScore )
    {
      // Computer's Toss
      compToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ;
      System.out.println("The Computer tosses: " + compToss);
      
      // Player's Toss
      System.out.print("Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? ");
      String numDice = scan.nextLine();
      
      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 );
      }
      
      // Determine Winner of Round and Adjust Scores
      if ( playerToss>compToss )
      {
        playerScore = playerScore+1;
        System.out.println("You win the round!");
      }
      else
      {
        compScore = compScore+1;
        System.out.println("You loose the round!");
      }      
      System.out.println("Score: computer " + compScore + ", you " + playerScore + "/n");
    }
    
    // Determine Winner of the Game
    System.out.println("Final Score: computer " + compScore + ", you " + playerScore);
    
    if ( compScore > playerScore ) 
      System.out.println("The Computer Wins!");
    else
      System.out.println("You Win the Game!");
    
  }
}

The sections of the program match the boxes in the flowchart.

QUESTION 19:

Will one player or the other eventually win? Or is a draw possible?