如何处理多个字符串值并将这些字符串值与用户输入进行比较?

How to deal with multiple string values and compare those string values with the user input?

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;

public class LetterRandomiser : MonoBehaviour
{
    public char[] characters; 

    public Text textbox; 

    public InputField mainInputField;

    public string[] lines;

    void Start() 
    {
        char c = characters[Random.Range(0, characters.Length)];
        char d = characters[Random.Range(0, characters.Length)];

        textbox.text = c.ToString() + d.ToString(); 

        lines = System.IO.File.ReadAllLines(@"C:\Users\lluc\Downloads\corncob_lowercase.txt");
    }

    void wordIsInFile(string word)
    {
        foreach (var item in lines)
        {
            if (word == item)
            {
                char c = characters[Random.Range(0, characters.Length)];
                char d = characters[Random.Range(0, characters.Length)];
                textbox.text = c.ToString() + d.ToString();
                mainInputField.text = "";
            }
            else
            {
                mainInputField.text = "";
            }
        }
    }

    void Update()
    { 
        if (Input.GetKeyDown(KeyCode.Return) && mainInputField.text.Contains(textbox.text) == true)
        {
            wordIsInFile(mainInputField.text);
        }
        else if (Input.GetKeyDown(KeyCode.Return) && mainInputField.text.Contains(textbox.text) == false)
        {
            wordIsInFile(mainInputField.text);
        }
    }
}

Reference to my game

我的问题:

我想让玩家猜一个包含两个随机生成字母的单词(显示在输入字段上方)。

但是现在,玩家可以输入一个只包含随机生成的两个字母中的一个的单词,程序仍然会接受他们的输入。

我不希望发生这种情况。我想对游戏进行编程,使其不接受玩家的输入,直到它们包含 两个随机生成的字母,而不仅仅是一个。

例如,在我向您展示的图像中,我可以输入 'joker',它会接受输入并生成一组新的随机生成的字母。但是小丑只有 'j' 而不是 'z' 所以它不应该是正确的。唯一应该接受的词是同时包含 J 和 Z 的词,即 jazz

您可以使用 String.Contains().

假设一组字母在一个字符串中进行比较:

string lettersToCompare;

并且您的用户输入是另一个字符串,例如:

string userInput;

您可以这样做:

string lettersToCompare;
string userInput;

bool isMatchingAllCharacters = true;
//Compare each character of the string
foreach (char c in lettersToCompare.ToCharArray())
{
    //if the userInput do not contain this character, you already now it won't match every character.
    if (!userInput.Contains(c.ToString()))
    {
        isMatchingAllCharacters = false;
        break;
    }
}

if (isMatchingAllCharacters)
{
    Debug.Log("All matched");
}
else
{
    Debug.Log("Something do not match");
}

如果您使用 LinQ:

,则可以避免 char-string 转换
using System.Linq;

string lettersToCompare;
string userInput;

bool isMatchingAllCharacters = true;
//Compare each character of the string
foreach (char c in lettersToCompare)
{
    //if the userInput do not contain this character, you already now it won't match every character.
    if (!userInput.Contains(c))
    {
        isMatchingAllCharacters = false;
        break;
    }
}

if (isMatchingAllCharacters)
{
    Debug.Log("All matched");
}
else
{
    Debug.Log("Something do not match");
}

使用 System.Linq() 中的 Intersect() 怎么样?

string
    generatedLetters = "jz",
    userInput = "joker";

if (generatedLetters.Intersect(userInput).Count() == userInput.Length)
{
    //yes! good!
}
else
{
    //too bad..
}

Intersect() 字符数必须与 generatedLetters 字符数匹配才能满足您的条件。