第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取字符串中兩個相同字符的索引?

如何獲取字符串中兩個相同字符的索引?

C#
白衣非少年 2022-01-09 16:31:19
我想創(chuàng)建簡單的控制臺僚機游戲。我的錯誤是,如果我嘗試在單詞中獲取 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!");            }        }    }    }
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經(jīng)驗 獲得超4個贊

您需要處理可能是不同大小寫等的用戶輸入。因此,最簡單的方法是只訪問隨機單詞中的每個字符一次。


這是我為解決此問題而制作的 REPL:


using System;

using System.Collections.Generic;


class MainClass {

    public static void Main (string[] args) {

        var word = "Tomato";

        var input = "t";

        var letter = input.ToLower()[0];


        var indices = new List<int>();

        for(var i = 0; i < word.Length; i++)

            if (word.ToLower()[i] == letter)

                indices.Add(i);


        Console.WriteLine($"Secret word: {word}");

        Console.WriteLine($"User guess: {input}");

        Console.WriteLine($"Found at {String.Join(", ", indices)}");

    }

}

及其輸出:


Mono C# compiler version 4.0.4.0

Secret word: Tomato

User guess: t

Found at 0, 4


查看完整回答
反對 回復(fù) 2022-01-09
  • 1 回答
  • 0 關(guān)注
  • 239 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號