空の推測をするとインデックスが範囲外になりますが、スコアラベルの更新にも問題があります。現在、更新は行われますが、フォームでは実行されません。私はまた、ユーザーがより多くのポイントを得るために有効な文字を入力保つことができないことを確認したいのですが、助けてくださいはちょうどこのように、バックのC#やプロジェクトを学ぶようになった私はC#Hangman with Bugs
namespace Guess_The_Word
{
public partial class Form1 : Form
{
private int wrongGuesses = 0;
private int userGuesses;
private int score = 0;
private string secretWord = String.Empty;
private string[] words;
private string currentWord = string.Empty;
private string userGuess = string.Empty;
private string userInput = string.Empty;
private string randomInput = string.Empty;
public Form1()
{
InitializeComponent();
}
private void guessBtn_Click(object sender, EventArgs e)
{
string guess = userInputBox.Text.ToString();
char[] randomCharArray = currentWord.ToLowerInvariant().ToCharArray();
char[] userInputArray = guess.ToLowerInvariant().ToCharArray();
//Assume that userInput would never be superior than randomCharArray
//And contain only one char
for (int i = 0; i < randomCharArray.Length; i++)
{
if (userInputArray[0].Equals(randomCharArray[i])) // Here is where the error happens
{
UpdateScore();
}
}
// Clean userInput in form
userInputBox.Text = string.Empty;
}
private void resetGamebtn_Click(object sender, EventArgs e)
{
SetUpWords();
}
private void SetUpWords()
{
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];
wordlbl.Text = string.Empty;
for (int i = 0; i < currentWord.Length; i++)
{
wordlbl.Text += "*";
}
}
private void UpdateScore()
{
scorelbl.Text = Convert.ToString(score);
}
}
}
は、エラーを修正し、コード –