【发布时间】:2018-03-12 10:24:41
【问题描述】:
所以我正在制作一个刽子手游戏来练习我的 C# 技能,但我遇到了一个问题,我无法弄清楚如何循环遍历随机选择的单词并比较用户在文本框中的选择。我知道如何获取用户输入并将其转换为变量字符串。但不是如何比较它并删除 * 并用正确的字母替换它。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Guess_The_Word
{
public partial class Form1 : Form
{
private int wrongGuesses = 0;
private int userGuesses;
private string secretWord = String.Empty;
private string[] words;
private string currentWord = string.Empty;
private string userGuess = string.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string path = (@"C:\commonwords.txt"); // Save the variable path with the path to the txt file
words = File.ReadAllLines(path);
int guessIndex = (new Random()).Next(words.Length);
currentWord = words[guessIndex];
char[] currentWordCharArray = currentWord.ToCharArray();
foreach (char c in currentWordCharArray)
{
currentWord.Contains(userInputBox.Text);
wordlbl.Text += "*";
}
}
private void guessBtn_Click(object sender, EventArgs e)
{
}
private void resetGamebtn_Click(object sender, EventArgs e)
{
}
}
}
【问题讨论】:
-
看起来您可能还需要将用户输入转换为 char 数组,然后将随机单词的第一个索引与用户输入框的第一个索引进行比较,并对其他人。
-
我已经把用户的猜测变成了char数组,我会用什么方法来比较两个char数组呢?
-
也许我跳得太远了,或者我需要考虑一下,明天再回顾一下。
-
我想问题是您是一次检查整个单词?还是您正在逐字输入?,用户输入的字母或单词是什么?
-
在下面添加了一个示例,以便您可以从中构建一个示例
标签: c# string search compare substring