0

我的一位教授给了我们一个创建随机数猜谜游戏的项目。对游戏的要求是至少要有一个公共属性和一个公共方法,然后才是循环的实际规则。游戏还必须至少包含两个类别。我将列出我现有的代码,希望有人可以指导我正确的方向。当它运行时,它只运行关于规则的 console.writeline 消息。然后它停止。循环不会迭代,我放在非测试类中的“方法”。从一开始我就一直在努力学习这门课,因为我以前没有编程经验。我看到的这个游戏的大多数例子只涉及一个类。所以我不完全确定我应该在另一个里面放什么,

我的代码如下,首先是非测试类。

using System;

namespace numbergame
{
class mysteryNumber
{
    public int myRand {get; set;}
    public int playerGuess { get; set; }

    public void DisplayMessage()
    {
        Console.WriteLine("The most important rule of the Number Guessing Game is that \nyou can't talk about the Number Guessing Game."); 
    }

下一段代码是我拥有大部分代码和主要方法的地方。

using System;

namespace numbergame
{
    class mysteryNumberTest
    {
        static void Main(string[] args)
        {
            int myRand;
            int guessCounter = 1;
            int playerGuess = 0;

            Console.WriteLine("The number you must choose is a number between \n1 and 100. Your attempts remaining will be \nshown upon incorrect guess. The number of attempts it took you will be\n shown upon correct guess.");


            Random randomNumbers = new Random();
            myRand = randomNumbers.Next(1, 100);

            {
                while (guessCounter < 5) ;
                Console.WriteLine("Feeling lucky, punk? Guess your number: ");
                playerGuess = Convert.ToInt32(Console.ReadLine());

                {
                    if (playerGuess == myRand)
                        Console.WriteLine("Congratulations! You're a real winner!\n");
                    Console.WriteLine("The number of attempts it took you was: {0}", guessCounter);
                    {
                        guessCounter = guessCounter + 10; }
                        if (playerGuess != myRand)
                        { guessCounter += 1; }
                        Console.WriteLine("No. You have made {1} attempts.\nYou have {2} attempts left.", guessCounter, (5 - guessCounter));
                    if (guessCounter >= 6)
                        Console.WriteLine("The number of guesses was exceeded.");
                }
            }
        }
    }
}

第一个非测试类中的实例变量和自动化属性似乎毫无用处。DisplayMessage() 方法似乎也可能实际上不起作用。

但我不知道。我不确定为什么我的循环没有开始,当它开始时,我感觉我的guessCounter 可能不会按照我想要的方式递增,因为我在上周的练习中遇到了这个问题。

任何帮助表示赞赏。:(

4

3 回答 3

0

您忘记了它会出现的一些 {}。

它去了

while (test) 
{ // <- Notice the bracket
//Code to repeat 5 times goes here.
} // <- it closes

另外:如果您希望它重复 5 次,请将guessCounter 初始化为 0。

于 2013-10-23T01:51:06.663 回答
0

我将使用只读属性创建您的辅助类,该属性在构造函数中使用随机数进行初始化。像这样的东西:

public class Mystery
{

    private static Random R = new Random();

    private int _number;
    public int Number 
    {
        get { return _number; }
    }

    public Mystery()
    {
        _number = R.Next(1, 101); // 1 to 100 INCLUSIVE
    }

    public void DisplayMessage()
    {
        Console.WriteLine("The most important rule of the Number Guessing Game is that \nyou can't talk about the Number Guessing Game.");
        Console.WriteLine("The number you must choose is a number between \n1 and 100. Your attempts remaining will be \nshown upon incorrect guess. The number of attempts it took you will be\n shown upon correct guess.");
    }

}

请注意,如果您想要一个介于 1 和 100 之间的数字,那么 Random.Next() 的第二个参数应该是 101,而不是 100。

现在您可以在 Main() 中创建此类的实例并像这样使用它:

    static void Main(string[] args)
    {
        int guessCounter = 1;
        int playerGuess = 0;

        Mystery mystery = new Mystery();
        mystery.DisplayMessage();

        while (guessCounter < 5) 
        {
            Console.WriteLine("Feeling lucky, punk? Guess your number: ");
            playerGuess = Convert.ToInt32(Console.ReadLine());
            if (playerGuess == mystery.Number)
            {
                Console.WriteLine("Congratulations! You're a real winner!\n");
                Console.WriteLine("The number of attempts it took you was: {0}", guessCounter);
            }
            else
            {
                // ... do something else in here ...                    
            }
        }
    }

当猜测不正确时,我不太遵守规则。在一个地方你加 10,而在另一个地方你加 1。我会让你弄清楚“......在这里做其他事情......”部分。

于 2013-10-23T02:11:38.720 回答
0

首先,正如戴所说,有很多你不需要的匿名范围。

现在让我们看看你的老师问了什么。他想要2节课。1 个公共属性,1 个公共方法。我的猜测是公共方法验证输入的数字是否有效?

此外,while循环声明如下:

while(bCondition == true)
{
    // your code here
}

请注意您如何将循环声明为while(guessCounter < 5); (注意指令结束)。这就是它不“进入”循环的原因。

现在,这样的事情会更好:

while(numberGuesses < 5)
{
    // We've entered the loop. 
    // We now want to know if the guess is good. If it is, tell the player then leave
    // the loop
    if(playerGuess == yourNumber)
    {
        Console.WriteLine("You guessed it right! Right after {0} guesses!", numberGuesses); 
        break; // Break leaves the loop it is in. Therefore our 'while'.
    }

    // No need for else instruction since you can't be here with the good guess
    // do what you must do here when he got it wrong

}

至于你的公共方法和属性,不能真正帮助你,因为我不明白他为什么总共需要 2 个类。但是,请考虑这样做:

anyNumber++; // correspond to : anyNumber = anyNumber + 1; 
// or
anyNumber += aNumber; // where a number can be any number. (anyNumber = anyNumber + aNumber)

所以你可以做 numberGuesses++; 在循环结束时。至于其余的,我认为您应该尝试在纸上画出您的看法,或者您的老师会。

于 2013-10-23T02:00:03.870 回答