保持跟踪哪些用户输入 [英] Keeping track of what the user inputs

查看:132
本文介绍了保持跟踪哪些用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的刽子手code,它几乎好到哪里它显示的猜测字母点。只显示最新猜到信,但我希望它从什么不放过继续。比如,如果一个人想A,然后选择L然后猜字母是A,L。
我试过后,猜字母是但它给我的输出使用for循环
A,A,A,A,A。我应该怎么做才能解决这个问题?

 类刽子手
{
    公共字符串[]字=​​新的字符串[5] {阵,对象,类,循环,HUMBER};
    公共字符串[] =折磨新的字符串[6] {左臂,右臂,左腿,右腿,体,头};
    公众的char [] =猜测新的char [26];
    INT I;    公共无效randomizedWord()
    {        随机随机=新的随机();        INT指数= random.Next(0,5);
        的char [] =隐藏着新的char [字[指数]。长度]
        串字=字[指数]
        Console.WriteLine(字[指数]);        Console.Write(这个词是:);
        对于(i = 0; I< hidden.Length;我++)
        {
            Console.Write(' - ');
            隐藏[我] =' - ';
        }
        Console.WriteLine();        INT生活= 6;
        做
        {
            Console.WriteLine(猜一个字母:);
            炭userinput =到Console.ReadLine()ToCharArray()[0];
            指数++;
            猜[指数] = userinput;
            Console.WriteLine(猜字母是:+猜[指数]);            布尔foundLetter = FALSE;
            的for(int i = 0; I< hidden.Length;我++)
            {
                如果(字[我] == userinput)
                {
                    隐藏[I] = userinput;
                    foundLetter = TRUE;
                    Console.WriteLine(你猜对了!);
                }
            }
            为(中间体X = 0; X&下; hidden.Length; X ++)
            {
                Console.Write(隐藏[X]);
            }
            如果(!foundLetter)
            {
                Console.WriteLine(这不是一个正确的字母);
                lives--;                如果(住== 5)
                {
                    Console.WriteLine(你失去了一个+折磨[0]);
                }
                否则,如果(住== 4)
                {
                    Console.WriteLine(你失去了+折磨[1]);
                }
                否则,如果(住== 3)
                {
                    Console.WriteLine(你失去了你的+折磨[2]);
                }
                否则如果(住== 2)
                {
                    Console.WriteLine(你失去了+折磨[3]);
                }
                否则如果(住== 1)
                {
                    Console.WriteLine(你失去了你的+折磨[4]);
                }
                其他
                {
                    Console.WriteLine(你失去了你的+折磨[5]);
                    Console.WriteLine(你输了!);
                    打破;
                }
            }            布尔founddash = FALSE;
            对于(INT Y = 0; Y< hidden.Length; Y ++)
            {
                如果(隐藏[Y] ==' - ')
                {
                    founddash = TRUE;
                }
            }
            如果(!founddash)
            {
                Console.WriteLine(你赢了!);
                打破;
            }            Console.WriteLine();
        }而(生命!= 0);
    }


解决方案

在该行选择了随机字的首页变量被设置为一个随机数。

  INT指数= random.Next(0,5);

然后就是首页被用来猜测存储在do..while循环。然而,由于首页 0和5之间的随机数开始出现意外的行为。

在do..while循环前行设置索引为0,for循环(或其它地方提出的替代方案)应该工作例如如此

 指数= 0;
INT生活= 6;

{
    //将code休息

This is my hangman code, and its almost good up to the point where it displays the guessed letters. It only displays the most recent guessed letter but I want it to continue on from whats left off. Such as if a person guess "A" and then "L" then Guessed letters are A, L. I tried using a for loop after "Guessed letters are" but then it gives me the output "A, A, A, A, A". What should I do to fix this problem?

class Hangman
{
    public string[] words = new string[5] { "ARRAY", "OBJECT", "CLASS", "LOOP", "HUMBER" };
    public string[] torture = new string[6] { "left arm", "right arm", "left leg", "right leg", "body", "head" };
    public char[] guessed = new char[26];
    int i;

    public void randomizedWord()
    {

        Random random = new Random();

        int index = random.Next(0, 5);
        char[] hidden = new char[words[index].Length];
        string word = words[index];
        Console.WriteLine(words[index]);

        Console.Write("The word is: ");
        for (i = 0; i < hidden.Length; i++)
        {
            Console.Write('-');
            hidden[i] = '-';
        }
        Console.WriteLine();

        int lives = 6;
        do
        {
            Console.WriteLine("Guess a letter: ");
            char userinput = Console.ReadLine().ToCharArray()[0];
            index++;
            guessed[index] = userinput;
            Console.WriteLine("Guessed letters are: " + guessed[index]);

            bool foundLetter = false;
            for (int i = 0; i < hidden.Length; i++)
            {
                if (word[i] == userinput)
                {
                    hidden[i] = userinput;
                    foundLetter = true;
                    Console.WriteLine("You guessed right!");
                }
            }      
            for (int x = 0; x < hidden.Length; x++)
            {
                Console.Write(hidden[x]);
            }
            if (!foundLetter)
            {
                Console.WriteLine(" That is not a correct letter");
                lives--;

                if (lives == 5)
                {
                    Console.WriteLine("You lost a " + torture[0]);
                }
                else if (lives == 4)
                {
                    Console.WriteLine("You lost the " + torture[1]);
                }
                else if (lives == 3)
                {
                    Console.WriteLine("You lost your " + torture[2]);
                }
                else if (lives == 2)
                {
                    Console.WriteLine("You lost the " + torture[3]);
                }
                else if (lives == 1)
                {
                    Console.WriteLine("You lost your " + torture[4]);
                }
                else
                {
                    Console.WriteLine("You lost your " + torture[5]);
                    Console.WriteLine("You lose!");
                    break;
                }
            }

            bool founddash = false;
            for (int y = 0; y < hidden.Length; y++)
            {
                if (hidden[y] == '-')
                {
                    founddash = true;
                }
            }
            if (!founddash)
            {
                Console.WriteLine("  You Win!  ");
                break;
            }

            Console.WriteLine();
        } while (lives != 0);
    } 

解决方案

The index variable is being set to a random number when the random word is selected in this line.

int index = random.Next(0, 5);

Then that index is being used to store the guesses in the do..while loop. However, since index starts from a random number between 0 and 5 you see unexpected behaviour.

Set index to 0 in the line before the do..while loop and the for loop (or the alternatives suggested elsewhere) should work e.g. as so

index = 0;
int lives = 6;
do
{
    // rest of the code

这篇关于保持跟踪哪些用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆