Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 两类随机数游戏,不迭代。建议?_Class_Loops_Project - Fatal编程技术网

Class 两类随机数游戏,不迭代。建议?

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

我的一位教授给了我们一个创建随机数字猜测游戏的项目。游戏的要求是它至少有一个公共属性和一个公共方法,然后是循环的实际规则。游戏还必须至少包含两个类。我将列出我现有的代码,希望有人能指引我正确的方向。当它运行时,它只运行有关规则的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.");
                }
            }
        }
    }
}
使用系统;
名称空间numbergame
{
类mysteryNumberTest
{
静态void Main(字符串[]参数)
{
内特迈朗;
int猜测计数器=1;
int playerGuess=0;
Console.WriteLine(“您必须选择的数字是介于\n1和100之间的数字。如果猜测不正确,您剩余的尝试次数将\n显示出来。如果猜测正确,您的尝试次数将显示出来。”);
随机数=新随机数();
myRand=随机数。下一个(1100);
{
而(猜测计数器<5);
控制台。WriteLine(“感觉幸运吗,朋克?猜猜你的号码:”);
playerGuess=Convert.ToInt32(Console.ReadLine());
{
如果(playerGuess==myRand)
Console.WriteLine(“祝贺你!你是一个真正的赢家!\n”);
WriteLine(“尝试次数为:{0}”,guessCounter);
{
猜测计数器=猜测计数器+10;}
如果(playerGuess!=百万兰特)
{guessCounter+=1;}
WriteLine(“不,您已经进行了{1}次尝试。\n您还有{2}次尝试。”,guessCounter,(5-guessCounter));
如果(猜测计数器>=6)
Console.WriteLine(“超出了猜测的次数。”);
}
}
}
}
}
第一个非测试类中的实例变量和自动属性似乎没有任何用途。DisplayMessage()方法看起来也可能不起作用

但我不知道。我不确定为什么我的循环没有开始,当循环开始时,我感觉我的猜测计数器可能不会以我希望的方式递增,因为我在上周的练习中遇到了这个问题


非常感谢您的帮助(

你忘了它会出现的一些{}

就这样

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

{/首先,正如Dai所说,有很多匿名作用域是你不需要的

现在让我们看看你的老师问了什么。他想要两个类。一个公共属性,一个公共方法。我猜公共方法会验证输入的数字是否有效

此外,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

}

因此,您可以在循环结束时进行NumberGuesss++。至于其余部分,我假设您应该尝试在纸上画出您对它的看法,或者您的老师会这样做。

我将使用只读属性创建您的二级类,该属性在构造函数中用随机数初始化。类似这样的内容:

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.");
    }

}
    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 ...                    
            }
        }
    }
请注意,如果您想要一个介于1和100(包括1和100)之间的数字,则Random.Next()的第二个参数应该是101,而不是100

现在,您可以在Main()中创建该类的实例,并按如下方式使用它:

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.");
    }

}
    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 ...                    
            }
        }
    }
static void Main(字符串[]args)
{
int猜测计数器=1;
int playerGuess=0;
神秘=新的神秘();
DisplayMessage();
while(猜测计数器<5)
{
控制台。WriteLine(“感觉幸运吗,朋克?猜猜你的号码:”);
playerGuess=Convert.ToInt32(Console.ReadLine());
if(playerGuess==神秘号)
{
Console.WriteLine(“祝贺你!你是一个真正的赢家!\n”);
WriteLine(“尝试次数为:{0}”,guessCounter);
}
其他的
{
//…在这里做点别的。。。
}
}
}

当猜测不正确时,我不太遵守规则。在一个地方你加10,而在另一个地方你加1。我会让你算出“…在这里做点别的…”部分。

您的代码似乎使用了许多不必要的匿名作用域,好像它属于已被移动的控制语句,例如您的
while(guessCounter<5);
语句(实际上什么都不做)。我建议你自己仔细阅读你的代码。我还看到一些其他错误。学习如何使用循环和条件,不要再到StackOverflow来为你做功课。至少先用谷歌搜索一下。