C#将“IndexOf”作为一个字符串,只显示字符串的第一个字符,其位置为

时间:2016-02-15 00:00:26

标签: c# regex

我正在为一个班级编写一个编码问题,并且已经停留了大约2个小时试图通过这个部分。我需要朝着正确的方向轻推。我们正在Visual Studio中的控制台应用程序中输入类似死亡的游戏。现在我正在尝试获取用户输入的字符,以便在与显示的短语中的字符匹配时显示为正确。

    // attributes
    string zombie;
    string phrase;
    int length;
    public void PlayingGame()
    {
        while (playerhealth > 0)
            {
                if (zombie == null)
                {
                    //load random zombie and phrasse
                    zombie = data.RandomZombie();
                    phrase = data.RandomPhrase();
                    //reset index and timer
                    zombieTimer = 0;
                    letterIndex = 0;
                    //displays zombie and phrase
                    Console.WriteLine(zombie);
                    Console.WriteLine(phrase);
                    length = phrase.Length;
                    // given loop info
                    while (Console.KeyAvailable==false) 
                    {
                        //reads characters entered
                        ConsoleKeyInfo key = Console.ReadKey();
                        string letter = key.KeyChar.ToString().ToUpper();

                        // checks to see if the letter position from the   Indexof is equal to the position in the phrase
                        if (phrase.IndexOf(letter) == letterIndex)
                        {   
                            // displays! if correct                             
                            Console.Write("!");
                            // moves to the next letter
                            letterIndex++;
                        }
                        else
                        {   
                            // displays :( if wrong
                            Console.Write(":( restart phrase ");
                            //resets the Index to start phrase over
                            letterIndex = 0;
                        }


                    }

我已经用它做了一些测试,当我运行程序时,我的僵尸出现了短语显示,我可以在短语中键入第一个字符,它将显示为正确。用“!”表示。然而,在我尝试取索引后的第一个字符后,它表示= = -1。但是,如果第二个字符是空格(“”),它将看到正确。另外我的一个短语中有数字,而IndexOf找到了数字的位置。

我的测试类是:

ZombieData zombies = new ZombieData();
        zombies.LoadZombies();
        zombies.LoadPhrases("phrases.txt");
        Game game = new Game();
        game.PlayingGame();

我还有一个类,其中的方法可以从文件中获取僵尸和短语的数据,然后返回随机的僵尸和短语,以便在游戏中使用。

1 个答案:

答案 0 :(得分:1)

好的抱歉有时候只是写出来有助于看到问题。我稍微更改了代码并意识到我的问题是什么。谢谢你的聆听。

 public void PlayingGame()
    {
        while (playerhealth > 0)
            {
                if (zombie == null)
                {
                    //load random zombie and phrasse
                    zombie = data.RandomZombie();
                    phrase = data.RandomPhrase();
                    //reset index and timer
                    zombieTimer = 0;
                    letterIndex = 0;
                    //displays zombie and phrase
                    Console.WriteLine(zombie);
                    Console.WriteLine(phrase);
                    length = phrase.Length;
                    // given loop info
                    while (Console.KeyAvailable==false) 
                    {
                        //reads characters entered
                        ConsoleKeyInfo key = Console.ReadKey();
                        string letter = key.KeyChar.ToString().ToUpper();

                        // checks to see if the letter position from the Indexof is equal to the position in the phrase
                        // changed this to directly compare the letters instead of seeing if it was in the right porition and realized the problem was that I didn't realize the loop my instructor gave me was using ToUpper and making all the characters uppercase
                        if (letter == phrase[letterIndex].ToString().ToUpper())
                        {   

                            // displays! if correct                             
                            Console.Write("!");
                            // moves to the next letter
                            letterIndex++;
                        }
                        else
                        {
                            Console.WriteLine(letter);
                            Console.WriteLine(phrase[letterIndex].ToString());
                            // displays :( if wrong
                            Console.Write(":( restart phrase ");
                            //resets the Index to start phrase over
                            letterIndex = 0;
                        }


                    }



                }
            }
    }
}

}

我的讲师提供了Console.KeyAvailable while循环和字符串字母,我没有意识到问题是需要进行国会化的所有角色。