Write a game that performs the roll of two dice randomly. The game should first ask for a target sum. Everytime the player enters r, the program should repeatedly print the side of the dice on a separate line. The game should stop prompting if one of these two events occur: -The sum of the two sides is equal to the target sum. -The player enters q.

Respuesta :

Answer:

Explanation:

The following program was written in Java. It asks the user to first enter the target sum. Then it creates a while loop that continues asking the user to either roll dice or quit. If they decide to roll, it rolls the dice and prints out both values. If the target sum was hit, it prints that out and exits the program otherwise it loops again. The code has been tested and can be seen in the attached image below.

import java.util.Random;

import java.util.Scanner;

class Brainly {

   public static void main(String[] args) {

       Random rand = new Random();

       Scanner in = new Scanner(System.in);

       System.out.print("Enter Target Sum: ");

       int targetSum = in.nextInt();

       while (true) {

           System.out.println("r: Roll Again");

           System.out.println("q: Quit");

           String choice = in.next();

           char choiceModified = choice.toLowerCase().charAt(0);

           if (choiceModified == 'r') {

               int roll1 = rand.nextInt(6) + 1;

               int roll2 = rand.nextInt(6) + 1;

               int total = roll1 + roll2;

               System.out.println("Dice 1: " + roll1);

               System.out.println("Dice 2: " + roll2);

               if (total == targetSum) {

                   System.out.println("Target Sum of " + targetSum + " hit.");

                   break;

               }

           } else if (choiceModified =='q') {

               break;

           } else {

               System.out.println("Wrong choice");

           }

       }

   }

}

Ver imagen sandlee09
ACCESS MORE