猜猜用户选择最大值的游戏

时间:2016-09-22 18:01:47

标签: c# linq oop random

我的课程中有一个任务是使用c#和do / while,if / else循环创建一个随机数生成猜谜游戏。这项任务似乎很容易;然而,我遇到了问题,因为我应该通过提示用户输入你要猜测的最大值来开始游戏。#34;。 我不能让我的程序正常工作,因为每次运行它时,无论我输入什么值,它都会指示"选择介于1和0之间的值"

我附上了我的exe。

        Random generator = new Random();
        bool truth = true;
        int MaxRange = Convert.ToInt32(truth); ;
        int userguess = 0;
        int outputnumber = generator.Next(MaxRange);

        do
        {
            Console.WriteLine(" Enter a max number you want to guess from!", MaxRange);
            Console.ReadLine();

            Console.WriteLine("please make a guess between 1 and {0}", outputnumber);

            if (userguess != outputnumber)
            {
                userguess = Convert.ToInt32(Console.Read());

                if (userguess < outputnumber)
                {
                    Console.WriteLine("That is not correct, Guess again");
                    Console.ReadLine();
                }
                if(userguess > outputnumber)
                {
                    Console.WriteLine("That is not correct, Guess again");
                    Console.ReadLine();
                }
                else if (userguess == outputnumber)
                {
 console.writeline("That is correct, the number is {0}, outputnumber);
                }
            }
        } while (truth == false);
        }
    }
}

4 个答案:

答案 0 :(得分:3)

你的代码中有很多错误,但我会通过在旁边写评论来查看我看到的主要错误。

Random generator = new Random();
bool truth = true;

//extra semicolon on next line, 
//doesn't make sense to convert a boolean to get your initial max range,
//just give it an initial value, perhaps 0
int MaxRange = Convert.ToInt32(truth); ;
int userguess = 0;

//the generator can't get the correct outputnumber yet because MaxRange hasn't
//been set by the user's input yet
int outputnumber = generator.Next(MaxRange);

do
{
    //this won't get the user input because the parameters in WriteLine() 
    //that come after the string are for inserting into the string, not recieving input
    Console.WriteLine(" Enter a max number you want to guess from!", MaxRange);

    //need to set Console.ReadLine() to a variable for it to be saved
    //should be MaxRange = Console.ReadLine(); (though that will throw an error if the user inputs anything except numbers)
    Console.ReadLine();

    //outputnumber hasn't been set to a true generated number yet
    Console.WriteLine("please make a guess between 1 and {0}", outputnumber);

    //outputnumber could be renamed to be more clear, I would suggest randomNumber
    //userguess is still 0 during the first loop, need to get the user's guess before comparing to the random number
    //also, could simplify these if/else statements to just a couple of them
    //you're also going to need a while loop somewhere in here to continue having the user
    //guess until they get it right
    if (userguess != outputnumber)
    {
        //should use Console.ReadLine()
        userguess = Convert.ToInt32(Console.Read());

        if (userguess < outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        if(userguess > outputnumber)
        {
            Console.WriteLine("That is not correct, Guess again");
            Console.ReadLine();
        }
        else if (userguess == outputnumber)
        {
//not indented correctly, missing correct capitalization and missing the right quotes on the string to be wrote
console.writeline("That is correct, the number is {0}, outputnumber);
        }
    }
//should just be while (true);
} while (truth == false);
}

}

}

答案 1 :(得分:1)

对开发不熟悉可能是压倒性的,很明显你还没有完全考虑过问题和你的解决方案。在开发新算法时,有时可以帮助评论您将使用的步骤,然后开始开发。它们可能是模糊的指导或更详细的步骤,但您会发现这是一种有用的做法,尤其是在处理复杂问题时。

以下示例可用于重写代码并重试。我还建议使用int.TryParse()而不是convert。

// loop 1
// get users expected max range
//   if not int
//      display message about invalid input
//   if int
//      break loop 1 to start guessing
// end loop 1
// generate random number for user to guess
// loop 2
// prompt user for their guess
// parse input and verify int
//   if not int
//      display message about invalid input
//      continue loop 2 skipping eval code to reprompt for input
// evaluate their guess
//   if matches
//      show success message
//      break loop 2
//   else
//      show failure message

答案 2 :(得分:0)

这有很多问题。

您正在使用&#39; true&#39;生成一个数字。作为最大值,而不是在从用户获取输入后生成数字。

您还需要在获得正确的号码后将真相设置为false。

您可能还希望他们对同一个数字进行多次猜测,因此您需要将输入移动到&#39; do&#39;

开头之上的最大范围内

花时间逐行​​阅读您的代码,并考虑如何在不同的点设置变量等。

答案 3 :(得分:0)

谢谢大家!我做了一些小的调整,现在我几乎完美了。我仍然得到的唯一错误是成功运行它时,会询问用户是否要再次播放。如果他们说“是”,则游戏会根据之前选择的最大值进行游戏,而不是输入新值。

命名空间Trythisagain {     课程     {

    public static int SelectedNumber = 0;
    public static Random ran = new Random();
    public static bool GameOver = false;
    public static int UserMaxValue = 0;



    static void Main(string[] args)

    {
        int UserNumber;
        SelectedNumber = ran.Next(0, UserMaxValue);

        do
        {


            Console.WriteLine("Enter a max number you want to guess from!");
            UserMaxValue = Convert.ToInt32(Console.ReadLine());

        do
            {


                Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue);
                UserNumber = Convert.ToInt32(Console.ReadLine());
                GuessNumber(UserNumber);

        } while (GameOver == false);
        } while (GameOver == false);

    }




    public static void GuessNumber(int UserNumber)

    {

        int playagain = 0;

        if (UserNumber < SelectedNumber)
            Console.WriteLine("Your Number is Wrong! Please try Again!");
        else if (UserNumber > SelectedNumber)
            Console.WriteLine("Your Number is Wrong! Please Try Again!");
        else
        {

            Console.WriteLine("Yay! You got the right number! Press 1 to play again press 2 to quit");
            playagain = Convert.ToInt32(Console.ReadLine());

            while (playagain != 1 && playagain != 2)

            {
                Console.WriteLine("Please Only Select 1 to play again or 2 to quit!");
                playagain = Convert.ToInt32(Console.ReadLine());

            }

            if (playagain.Equals(2))
                GameOver = true;

            else
                SelectedNumber = ran.Next(0, UserMaxValue);
        }
    }
}

}