102k views
4 votes
Write a program in c# that simulates the roll of two six sided dice.

The program should ask the user to guess the total sum of the dice. The user has to
keep guessing until they get the total correct, and the program should tell them how
many tries it took.

An example of the expected output is shown below.
What do you think the total of two six-sided die will be? 6
The total was 8. Try again!
What do you think the total of two six-sided die will be? 4
The total was 11. Try again!
What do you think the total of two six-sided die will be? 10
The total was 10. Congratulations! It took you 3 tries to guess successfully.

1 Answer

7 votes

Answer:

using System;

class Program

{

public static void Main(string[] args)

{

Random rnd = new Random();

int tries = 0;

while (true)

{

tries++;

Console.Write("What do you think the total of two six-sided die will be? ");

int guess = int.Parse(Console.ReadLine());

int total = rnd.Next(1, 7) + rnd.Next(1, 7);

Console.Write($"The total was {total}. ");

if (total == guess)

{

Console.WriteLine($"Congratulations! It took you {tries} tries to guess successfully.");

break;

}

else

{

Console.WriteLine("Try again!");

}

}

}

}

Step-by-step explanation:

Worst case you could be guessing forever...

User Xpusostomos
by
5.0k points