随机数猜测游戏C#

时间:2014-07-07 06:58:11

标签: c#

我的代码目前有两个问题,我似乎无法弄清楚如何解决,他们以前工作过,但现在他们根本不工作。

我遇到的问题是,当用户得到正确答案时,它不会显示对话框,询问他们是希望结束还是再次播放。如果我在用户没有猜到猜测量中的正确数字时进入该对话,并且用户选择y(是)继续,那么它就不会挂起。

这是我目前所拥有的,因为如果我能弄清楚这一点,那么其他的应该更容易。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumberGame
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Random gen = new Random();
            int number;
            bool over = false;
            string userChoice;
            int guess = 0;
            int counter = 0;

            while (!over)
            {
                over = true;
                Console.WriteLine("1 - Easy: 1 - 10");
                Console.WriteLine("2 - Medium: 1 - 50");
                Console.WriteLine("3 - Hard: 1 - 100");
                Console.Write("Please input the number of the difficulty you wish to  play: ");
            string str = Console.ReadLine();

            switch (str)
            {
                case "1":
                    Console.WriteLine("You chose easy difficulty.");
                    number = gen.Next(1, 11);

                    Console.WriteLine("I'm thinking of a number between 1 - 10");
                        for (guess = Convert.ToInt32(Console.ReadLine()); guess != number; guess = Convert.ToInt32(Console.ReadLine()))
                        {
                            if (guess < 1 || guess > 10)
                            {
                                Console.WriteLine("Please enter a number between 1 - 10");
                            }
                            else if (guess < number)
                            {
                                Console.WriteLine("Higher");
                                counter++;
                                Console.WriteLine("You have " + (5 - counter) + " attempts left");
                            }
                            else if (guess > number)
                            {
                                Console.WriteLine("Lower");
                                counter++;
                                Console.WriteLine("You have " + (5 - counter) + " attempts left");
                            }
                            else
                            {
                                    Console.WriteLine("You have guessed correctly! It only took you: " + number + " tries.");
                                    Console.WriteLine("Would you like to play again? (y/n)");
                                    userChoice = Console.ReadLine();
                                    if (userChoice == "n")
                                    {
                                        Console.WriteLine("Thanks for playing!");
                                        Environment.Exit(0);
                                    }
                                    else if (userChoice == "y")
                                    {
                                        over = false;
                                    }
                            }
                            if (counter == 5)
                            {
                                Console.WriteLine("Sorry, you didn't guess it under 5 attempts.");
                                Console.WriteLine("Would you like to play again? (y/n)");
                                userChoice = Console.ReadLine();
                                if (userChoice == "n")
                                {
                                    Console.WriteLine("Thanks for playing!");
                                    Environment.Exit(0);
                                }
                                else if (userChoice == "y")
                                {
                                    over = false;
                                }
                           }
                        }
                    break;

3 个答案:

答案 0 :(得分:2)

根据我的评论进行一些重组可能会对你有很大的帮助

Console.WriteLine("I'm thinking of a number between 1 - 10");
do
{
    while(!int.TryParse(Console.ReadLine(), out guess))
    {
        //not valid number - enter again
    }
    //insert your if statement about whether guess is correct
    if(counter >= 5)
    {
        //unable to guess in 5 moves
        break;
    }
}while(guess != number)
Console.WriteLine("Would you like to play again? (y/n)");
userChoice = Console.ReadLine();
if (userChoice == "n")
{
    over = true;
}

List<int> guesses = new List<int>()

bool guessed = true;
while(guessed)
{
    foreach(int g in guesses)
    {
         if(g == guess)
         {
             break;
         }
    }
    guesses.Add(guess);
    guessed = false;

}

答案 1 :(得分:1)

当您的猜测正确时,您没有进入for

for (guess = Convert.ToInt32(Console.ReadLine()); guess != number; guess = Convert.ToInt32(Console.ReadLine()))
                                                  ^^^^^^^^^^^^^^^

需要改进的选项:

1.保持for,但将其用作计数器,例如

for (var counter = 0; counter < 5; counter++)
{
    var guess = int.Parse(...);
    ... 
}
  

较小的变量范围可以简化调试(例如示例)   没有将计数器重置为零)。 - Luaan

2.用for

替换while
const int maxCount = 5;
int counter = 0;
while ((guess = int.Parse(Console.ReadLine()) != number && counter++ < maxCount)
{
    ...
}

答案 2 :(得分:0)

不确定,如果是主要问题,请尝试在设置continue后添加over = false语句并使用return代替Environment.Exit(0);