如何获取字符串中两个相同字符的索引?

我想创建简单的控制台僚机游戏。我的错误是,如果我尝试在单词中获取 2 个相同字符的 pos,我只会得到一个,而另一个被跳过。例如西红柿。


控制台输出:


番茄


_ 奥马特 _ _


我知道我没有使用 live 没有时间我做它层。


类程序{


   static string[] word = { "Pineapple", "Apple" , "Tomatoe" , "Pizza"};


    static int wordIndex = 0;


    static char[] randomWord;


    static bool guessing = true;


    public static void Main(string[] args)


    {

        int lives = 3;

        Console.OutputEncoding = Encoding.UTF8;

        Console.InputEncoding = Encoding.UTF8;

        Random r = new Random();

        wordIndex = r.Next(word.Length);

        randomWord = word[wordIndex].ToLower().ToCharArray();

         char[] randomWordcensored = new char[randomWord.Length];

        for (int i = 0; i < randomWord.Length; i++)

        {

            randomWordcensored[i] = '_';

        }

        Console.WriteLine("Hello");

        foreach (var item in randomWordcensored)

        {

            Console.Write(item + " ");

        }

        Console.WriteLine();

        Console.WriteLine("Please Enter character:");


        while (guessing = true)

        {

            int g = 0;

            char userinput;

            bool security = char.TryParse(Console.ReadLine() ,out userinput);

            if (security == true) { 


            if (randomWord.Contains(userinput))

            {        //help needed

                    g = (word[wordIndex].ToString().IndexOf(userinput) == -1  ? 0 : word[wordIndex].ToString().IndexOf(userinput));

                    randomWordcensored[g] = userinput;

                Console.WriteLine("Good :) " + g);

                    foreach (var item in randomWordcensored)

                    {

                        Console.Write(item + " ");

                    }

            }


            else

            {

                    lives--;

                Console.WriteLine("Wrong!\n-Lives:" + lives);


            }


        }

            else

            {

                Console.WriteLine("Enter only one charracter!");

            }

        }


    }



    }


白衣非少年
浏览 248回答 1
1回答

蝴蝶不菲

您需要处理可能是不同大小写等的用户输入。因此,最简单的方法是只访问随机单词中的每个字符一次。这是我为解决此问题而制作的 REPL:using System;using System.Collections.Generic;class MainClass {&nbsp; &nbsp; public static void Main (string[] args) {&nbsp; &nbsp; &nbsp; &nbsp; var word = "Tomato";&nbsp; &nbsp; &nbsp; &nbsp; var input = "t";&nbsp; &nbsp; &nbsp; &nbsp; var letter = input.ToLower()[0];&nbsp; &nbsp; &nbsp; &nbsp; var indices = new List<int>();&nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0; i < word.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (word.ToLower()[i] == letter)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indices.Add(i);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"Secret word: {word}");&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"User guess: {input}");&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"Found at {String.Join(", ", indices)}");&nbsp; &nbsp; }}及其输出:Mono C# compiler version 4.0.4.0Secret word: TomatoUser guess: tFound at 0, 4
打开App,查看更多内容
随时随地看视频慕课网APP