Photo AI
Question 16
Question 16 is about a dice game played against a computer. The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score... show full transcript
Step 1
Answer
using System;
class DiceGame
{
static void Main(string[] args)
{
Random rand = new Random();
int score = 0;
string userResponse;
do
{
int die1 = rand.Next(1, 7);
int die2 = rand.Next(1, 7);
int totalRoll = die1 + die2;
score += totalRoll;
Console.WriteLine($"Roll 1: {die1}");
Console.WriteLine($"Roll 2: {die2}");
Console.WriteLine($"Current score: {score}");
if (score < 21)
{
Console.WriteLine("Would you like to roll again? (yes/no)");
userResponse = Console.ReadLine();
}
else
{
userResponse = "no";
}
} while (userResponse.ToLower() == "yes");
if (score == 21)
{
Console.WriteLine("Congratulations! You have won!");
}
else if (score > 21)
{
Console.WriteLine("You lost!");
}
else
{
int randomScore = rand.Next(15, 22);
if (randomScore > score)
{
Console.WriteLine("You lost!");
}
else
{
Console.WriteLine("Congratulations! You have won!");
}
}
}
}
Report Improved Results
Recommend to friends
Students Supported
Questions answered