上下游戏。

时间:2014-01-03 01:07:17

标签: c# properties

(C#)这个游戏将有一台计算机生成一个随机数,用户将不得不继续猜测,直到他们发现它是什么。我的问题是我必须显示最佳分数,这是他们玩的所有时间中最低的猜测量,我不知道如何使用属性来做。

class OverUnder
{
    string playAgain = "";

    Random random = new Random();
    int randomNumber = random.Next(1, 101);

    int guess = 1;
    int userGuess;



    public string playerGuess()
    {
        Console.WriteLine("Guess a number from 1 - 100");
        string playerGuess = Console.ReadLine();
        return playerGuess;
    }
    public string userPlayAgain()
    {
        Console.WriteLine("Do you want to play again? Yes or No");
        string userPlayAgain = Console.ReadLine();
        return userPlayAgain;
    }
    public void playGame()
    {

        Console.WriteLine(randomNumber);

        do
        {
            userGuess = Convert.ToInt32(playerGuess());


            if (randomNumber > userGuess)
            {
                Console.WriteLine("Your guess is LOW");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + guess);
                guess++;
                Console.ReadLine();
            }

            else if (randomNumber < userGuess)
            {
                Console.WriteLine("Your guess is HIGH");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + guess);
                guess++;
                Console.ReadLine();
            }

            if (userGuess == randomNumber)
            {
                Console.WriteLine("You got it! Congratulations, You Win!");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + guess);
                Console.ReadLine();
                playAgain = userPlayAgain();
            }
        } while (userGuess != randomNumber || playAgain == "Yes" );
    }

显示计算机随机进行测试的内容。

2 个答案:

答案 0 :(得分:0)

只需在课堂上创建一个字段即可保持最佳分数:

class OverUnder
{
    int bestScore = Int.MaxValue; // initally max to reset it after the first game

    ...
}

然后当用户猜测时,设置其值。不要忘记将猜测次数重置为零。

    if (userGuess == randomNumber)
        {
            if (guess < bestScore)
                bestScore = guess;
            Console.WriteLine("You got it! Congratulations, You Win!");
            Console.WriteLine("# of guesses: " + guess);
            Console.WriteLine("Best score: " + bestScore.Value);
            Console.ReadLine();
            playAgain = userPlayAgain();
            guess = 1;
        }

答案 1 :(得分:0)

您可以创建另一个变量,当用户猜到随机数时,该变量将被保留并设置。

类似的东西。

class OverUnder
{
    string playAgain = "";

    Random random = new Random();

    public OverUnder()
    {
        randomNumber = random.Next(1, 101);
    }

    int randomNumber = 0;
    int bestScore = int.MaxValue;
    int guess = 1;
    int userGuess;


    public string playerGuess()
    {
        Console.WriteLine("Guess a number from 1 - 100");
        string playerGuess = Console.ReadLine();
        return playerGuess;
    }

    public string userPlayAgain()
    {
        Console.WriteLine("Do you want to play again? Yes or No");
        string userPlayAgain = Console.ReadLine();
        return userPlayAgain;
    }

    public void playGame()
    {

        Console.WriteLine(randomNumber);

        do
        {
            userGuess = Convert.ToInt32(playerGuess());


            if (randomNumber > userGuess)
            {
                Console.WriteLine("Your guess is LOW");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + guess);
                guess++;
                Console.ReadLine();
            }

            else if (randomNumber < userGuess)
            {
                Console.WriteLine("Your guess is HIGH");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + guess);
                guess++;
                Console.ReadLine();
            }

            if (userGuess == randomNumber)
            {
                if (guess < bestScore)
                    bestScore = guess;

                Console.WriteLine("You got it! Congratulations, You Win!");
                Console.WriteLine("# of guesses: " + guess);
                Console.WriteLine("Best score: " + bestScore);
                Console.ReadLine();
                playAgain = userPlayAgain();
            }
        } while (userGuess != randomNumber || playAgain == "Yes");
    }
}

注意我确实修复了一些编译器错误,但是你会看到设置为max int值的字段bestScore。当玩家猜出正确的数字时,你会检查他们的猜测数是否低于他们的最佳分数。如果是,则将simpleScore设置为猜测量。

干杯。