Вот код, который я пытался запустить и получил исключение «За пределами границ»:
Код: Выделить всё
using System;
class Guess
{
static void Main (string[] args)
{
Dictionary Door = new Dictionary();
Door.Add(1, "Nothing");
Door.Add(2, "Car");
Door.Add(3, "Nothing");
Random rng = new Random();
int x = rng.Next(0, Door.Count);
int doors = Door.Keys.ElementAt(x);
string prize = Door.Values.ElementAt(x);
int n = Door.Count;
int PrizeDoor = Convert.ToInt32(new Random());
Console.WriteLine("Which door do you want to select?");
int SelectedDoor = Convert.ToInt32(Console.ReadLine());
int RevealedDoor = Convert.ToInt32(new Random());
Console.WriteLine("Do you want to change your selection?");
string selection = Console.ReadLine();
if (selection == "Yes")
{
// ask user which new door to choose
Console.WriteLine("Which door would you like to choose now?");
int NewSelectedDoor = Convert.ToInt32(Console.ReadLine());
if (NewSelectedDoor == SelectedDoor)
{
Console.WriteLine("You cannot select the same door twice. Please try again.");
}
else if (NewSelectedDoor == PrizeDoor)
{
Console.WriteLine("Congratulations! You won the game!");
return;
}
else
{
Console.WriteLine("Sorry, you lost the game!");
return;
}
}
else
{
// stay with same door and reveal what's in the door
if (SelectedDoor == PrizeDoor)
{
Console.WriteLine("Congratulations! You won the game!");
return;
}
else
{
Console.WriteLine("Sorry, you lost the game!");
return;
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... random-doo