我有这个HangMan游戏,我遇到了问题。当用户点击字母按钮时,它不会跳进我的代码中的if-sentence。 "电流"变量是一个字符串,按钮内容是一个字母,我想知道该猜测单词是否包含该用户点击的那个字母。即使我调试该代码,我也不会得到当前的"猜测方法中的变量(猜词)。谢谢大家!
public sealed partial class Hangman : Page
{
private string[] words;
private int WrongGuesses = 0;
private string copyCurrent;
private string current;
public Hangman()
{
InitializeComponent();
loadWords();
DisplayTheWord();
Hangman_OnLoaded();
}
private void loadWords()
{
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
words = new string[ReadWords.Length];
}
private string[] images =
{
"/Assets/hang1.png", "/Assets/hang2.png", "/Assets/hang3.png",
"/Assets/hang4.png", "/Assets/hang5.png"
};
public void PlayAgain_OnClick(object sender, RoutedEventArgs e)
{
WrongGuesses = 0;
BitmapImage Hangman2 = new BitmapImage();
Uri URL = new Uri(BaseUri, images[WrongGuesses]);
Hangman2.UriSource = URL;
picture.Source = Hangman2;
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
int NextNumber = new Random().Next(words.Length);
copyCurrent = "";
for (int i = 0; i < ReadWords[NextNumber].Length; i++)
{
copyCurrent += "_" + " ";
}
CopiedWord.Text = copyCurrent;
}
public void DisplayTheWord()
{
WrongGuesses = 0;
BitmapImage Hangman2 = new BitmapImage();
Uri URL = new Uri(BaseUri, images[WrongGuesses]);
Hangman2.UriSource = URL;
picture.Source = Hangman2;
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
int NextNumber = new Random().Next(words.Length);
copyCurrent = "";
current = ReadWords[NextNumber];
for (int i = 0; i < ReadWords[NextNumber].Length; i++)
{
copyCurrent += "_" + " ";
}
CopiedWord.Text = copyCurrent;
}
public void Hangman_OnLoaded()
{
const int btnSize = 35;
var c = 0;
for (var i = 65; i <= 90; i++)
{
var btn = new Button {
Content = (char) i,
};
btn.Width = btn.Height = btnSize;
var margin = btn.Margin;
margin.Left = c += 37;
btn.Margin = margin;
GridMain.Children.Add(btn);
btn.Click += Guessing;
}
}
private void Guessing(object sender, RoutedEventArgs e)
{
Button choice = sender as Button;
var ltr = choice.Content.ToString();
if (current.Contains(ltr)) // it wont jump into this if sentence
{
char[] temp = copyCurrent.ToCharArray();
char[] find = current.ToCharArray();
char guessChar = ltr.ElementAt(0);
for (int index = 0; index < find.Length; index++)
{
if (find[index]== guessChar)
{
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
}
else
{
WrongGuesses++;
}
if (WrongGuesses < 6)
{
//picture.Source = muudab pilti
}
答案 0 :(得分:0)
为什么它不会在判刑时跳入?
没有跳转是因为您的current
变量为空或者不包含您提供的Itr
。
我得到ltr的值,但我没有得到当前值
我已经测试了关于获取current
值的代码,代码本身没有任何问题。但current
是从EnglishWords.txt
的随机行获得的字符串,如果随机行没有内容,则它将不包含任何内容。因此,请检查文本文件是否有空行和结果current = ReadWords[NextNumber];
。