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
- 1 回答
- 0 關(guān)注
- 239 瀏覽
添加回答
舉報