C# 检查字母位置,是否为正确的字母

C# 检查字母位置,是否为正确的字母,c#,.net,winforms,C#,.net,Winforms,我正在创建一个游戏,生成一个随机单词,然后让用户猜单词。 在用户输入他的单词后,游戏将比较它们,并返回正确的字母以及它们的位置是否正确。现在,当我输入同一个单词时,它会返回不同的结果 这就是我到目前为止所做的: class Game { public string CorrectPlace; public string WrongPlace; public string NoneExist; public string CorrectWord; publi

我正在创建一个游戏,生成一个随机单词,然后让用户猜单词。 在用户输入他的单词后,游戏将比较它们,并返回正确的字母以及它们的位置是否正确。现在,当我输入同一个单词时,它会返回不同的结果

这就是我到目前为止所做的:

class Game
{
    public string CorrectPlace;
    public string WrongPlace;
    public string NoneExist;
    public string CorrectWord;
    public string InputWord; // the word the uis
    public int points;

    public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" };

    public string getRandomWord(string names)
    {   
        Random ran = new Random();
        return Wordarray[(ran.Next(0,Wordarray.Length-1))];
    }

    public void CheckWords(string name)
    {
        InputWord.ToCharArray(); 

        CorrectWord = getRandomWord(CorrectWord); // store the random word in a string

        for (int i = 0; i < CorrectWord.Length; i++)
        {
            if (InputWord[i] == CorrectWord[i])
                MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!");
            else
                break;
        }  
    }
}

像这样更改代码。如果您有任何问题,请告诉我

班级游戏 {

公共字符串CorrectWord=null;
公共字符串InputWord;//UI中的单词
公共积分;
公共字符串[]Wordarray=新字符串[]{“雷达”、“安达”、“拉格”、“拉帕”、“拉塞尔”、“拉斯塔”、“里卡斯”};
公共字符串getRandomWord()
{
Random ran=新的Random();
返回Wordarray[(ran.Next(0,Wordarray.Length-1));
}
公共游戏
{
}
公共无效校验字(字符串名称)
{
char[]charInputWord=name.ToCharArray();
CorrectWord=getRandomWord();//将随机字存储在字符串中
Console.WriteLine(“随机字”+正确字);
Console.WriteLine(“用户名”+名称);
char[]charCorrectWord=CorrectWord.ToCharArray();
for(int i=0;i
名称和名称都从未被使用过,而且似乎是不必要的

看起来你想要这样的东西(转换成控制台打印,因为我没有你的UI源)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间文字游戏
{
班级游戏
{
静态void Main(字符串[]参数)
{
游戏g=新游戏();
bool gameOver=false;
而(!gameOver)
{
Console.WriteLine(“输入猜测(或键入“退出”以退出):”;
字符串answer=Console.ReadLine();
if(answer.ToLower()=“退出”)
打破
如果(例如,检查单词(答案))
{
Console.WriteLine(“你赢了!”);
while(true)
{
控制台。写入线(“再次播放(y/n)”;
答案=Console.ReadLine().ToLower();
如果(答案=“n”)
{
gameOver=true;
打破
}
否则如果(答案=“y”)
{
g、 选择andomword();
打破
}
}
}
}
}
公共字符串;
公共字符串[]Wordarray=新字符串[]{“雷达”、“安达”、“拉格”、“拉帕”、“拉塞尔”、“拉斯塔”、“里卡斯”};
private Random ran=新随机数();
公共游戏()
{
选择andomword();
}
公共void选择器andomword()
{
CorrectWord=Wordarray[(ran.Next(0,Wordarray.Length-1));
}
公共布尔校验字(字符串猜测)
{
if(guess.Trim()==CorrectWord)
{
返回true;
}
string guessLower=guess.Trim().ToLower();
字符串correctLower=CorrectWord.ToLower();
for(int i=0;i
试着这样做:

class Game
{
    public string CorrectPlace;
    public string WrongPlace;
    public string NoneExist;
    public string CorrectWord;
    public string InputWord;  
    public int points;

    public string[] Wordarray = null;

    public string getRandomWord(string names)
    {   
        Random ran = new Random();
        return Wordarray[(ran.Next(0,Wordarray.Length-1))];
    }

    public void Game()
    {
        Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }
        CorrectWord = getRandomWord(); // store the random word in a string
    }

    public void CheckWords(string name)
    {
        for (int i = 0; i < CorrectWord.Length; i++)
        {
            if (InputWord[i] == CorrectWord[i])
                MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!");
            else
                break;
        }  
    }
}
类游戏
{
公共场所;
公共场所;
公共字符串不存在;
公共字符串;
公共字符串输入字;
公共积分;
公共字符串[]Wordarray=null;
公共字符串getRandomWord(字符串名称)
{   
Random ran=新的Random();
返回Wordarray[(ran.Next(0,Wordarray.Length-1));
}
公开无效游戏()
{
Wordarray=新字符串[]{“雷达”、“安达”、“拉格”、“拉帕”、“拉瑟”、“拉斯塔”、“里卡斯”}
CorrectWord=getRandomWord();//将随机字存储在字符串中
}
公共无效校验字(字符串名称)
{
for(int i=0;i
幕后的想法是在游戏开始时获取一个随机单词(类
game
实例化的实例),然后将其与
CheckWords
方法中的用户输入进行比较

我建议不要调用
MessageBox。在循环中显示
,它将在每次匹配存在时弹出。

出现

您的行
InputWord.tocharray()未执行任何操作,结果将被丢弃。在您当前的代码中,它没有被使用,也不是必需的。您在哪里使用您的输入?我看不到输入字在任何地方被初始化。您正在与随机值进行比较,您的逻辑与您发布的代码不一致。因此,如果您对同一个单词进行比较,您的说法是错误的?在方法
getRandomWord
中,参数
名称
没有任何用处。
检查词的情况也一样。您如何调用
Checkwords
方法?我希望问题在这里:<
    public string CorrectWord = null;
    public string InputWord; // the word the uis
    public int points;

    public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" };

    public string getRandomWord()
    {
        Random ran = new Random();
        return Wordarray[(ran.Next(0, Wordarray.Length - 1))];
    }

    public void Newgame()
    {
    }

    public void CheckWords(string name)
    {

        char[] charInputWord = name.ToCharArray();


        CorrectWord = getRandomWord(); // store the random word in a string

        Console.WriteLine("Random Word " + CorrectWord);
        Console.WriteLine("User Word " + name);

        char[] charCorrectWord = CorrectWord.ToCharArray();

        for (int i = 0; i < charInputWord.Length; i++)
        {
            if (charInputWord[i] == charCorrectWord[i])
                Console.WriteLine("Letter #" + (i + 1).ToString() + " was correct!");
            else
                break;
        }
    }
}



class Program
    {
        static void Main(string[] args)
        {
            Game ab = new Game();
            ab.CheckWords("raser");

            Console.ReadLine();
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WordGame
{
    class Game
    {
        static void Main(string[] args)
        {
            Game g = new Game();
            bool gameOver = false;

            while (!gameOver)
            {
                Console.WriteLine("Enter a guess (or type 'exit' to exit):");
                string answer = Console.ReadLine();
                if (answer.ToLower() == "exit")
                    break;

                if (g.CheckWord(answer))
                {
                    Console.WriteLine("You win!");
                    while (true)
                    {
                        Console.WriteLine("Play Again (y/n)?");
                        answer = Console.ReadLine().ToLower();
                        if (answer == "n")
                        {
                            gameOver = true;
                            break;
                        }
                        else if (answer == "y")
                        {
                            g.ChooseRandomWord();
                            break;
                        }
                    }
                }
            }
        }

        public string CorrectWord;

        public string[] Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" };
        private Random ran = new Random();

        public Game()
        {
            ChooseRandomWord();
        }

        public void ChooseRandomWord()
        {
            CorrectWord = Wordarray[(ran.Next(0, Wordarray.Length - 1))];
        }

        public bool CheckWord(string guess)
        {
            if (guess.Trim() == CorrectWord)
            {
                return true;
            }

            string guessLower = guess.Trim().ToLower();
            string correctLower = CorrectWord.ToLower();

            for (int i = 0; i < correctLower.Length; i++)
            {
                if (guessLower[i] == correctLower[i])
                    Console.Write(guess[i]);
                else
                    Console.Write("#");
            }
            Console.WriteLine();

            return false;
        }

    }
}
class Game
{
    public string CorrectPlace;
    public string WrongPlace;
    public string NoneExist;
    public string CorrectWord;
    public string InputWord;  
    public int points;

    public string[] Wordarray = null;

    public string getRandomWord(string names)
    {   
        Random ran = new Random();
        return Wordarray[(ran.Next(0,Wordarray.Length-1))];
    }

    public void Game()
    {
        Wordarray = new string[] { "radar", "andar", "raggar", "rapar", "raser", "rastar", "rikas" }
        CorrectWord = getRandomWord(); // store the random word in a string
    }

    public void CheckWords(string name)
    {
        for (int i = 0; i < CorrectWord.Length; i++)
        {
            if (InputWord[i] == CorrectWord[i])
                MessageBox.Show("Letter #" + (i + 1).ToString() + " was correct!");
            else
                break;
        }  
    }
}