-1

我正在编写一个小游戏来练习自己,但我遇到了一个我无法修复的错误:S。这是一场悬疑游戏的开始。(不知道它的英文名称是否正确:))我需要从文件中取出一个单词,每行 1 个,并让玩家通过有限的尝试次数猜测该单词。

我认为我的错误与字符串/字符比较和操作有关,或者与我在文本标签中写的内容有关。我试图在互联网上找到一些已经解决的教程或问题,但没有什么和这个完全一样...... :(

我多次更改变量的类型,每行读取调试器行,但我从未发现有什么问题.. :S 如果你对 VB 有好处,请帮我解决这个问题:O(你也可以给出你的评论/改进)

谢谢,火焰

代码:

Imports System.IO
Public Class Pendu

Public Structure StructMot
    Public MotSecret() As Char
    Public LettreDecouverte() As Char
End Structure

Dim Mot As StructMot
Dim i As Integer = 0

Private Sub ButtonA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonA.Click, ButtonB.Click,
    ButtonC.Click, ButtonD.Click, ButtonE.Click, ButtonF.Click, ButtonG.Click, ButtonH.Click, ButtonI.Click, ButtonJ.Click,
    ButtonK.Click, ButtonL.Click, ButtonM.Click, ButtonN.Click, ButtonO.Click, ButtonP.Click, ButtonQ.Click, ButtonR.Click,
    ButtonS.Click, ButtonT.Click, ButtonU.Click, ButtonV.Click, ButtonW.Click, ButtonX.Click, ButtonY.Click, ButtonZ.Click

    i = i + 1
    ActiveControl.Visible = False
    PictureBox1.Image = ImageList1.Images(i - 1)
    Dim j As Integer = 0
    For j = 0 To Mot.MotSecret.Length - 1
        If ActiveControl.Text = Mot.MotSecret(j) Then
            Mot.LettreDecouverte(j) = Mot.MotSecret(j)
        End If
    Next j

    Label1.Text = ""
    For j = 0 To Mot.MotSecret.Length - 1
        Label1.Text = Label1.Text + " "
        If Mot.LettreDecouverte(j).Equals("") Then
            Label1.Text = Label1.Text + "_"
        Else
            Label1.Text = Label1.Text + Mot.LettreDecouverte(j)
        End If
    Next j

End Sub

Private Sub JouerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JouerToolStripMenuItem.Click
    GenereMot()
End Sub

Private Sub Pendu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    GenereMot()
End Sub

Function GenereMot()
    Dim NbItems As Integer
    Dim Aleatoire As New Random()
    Dim NbAleatoire As Integer
    ListBox1.Items.AddRange(System.IO.File.ReadAllLines("listemot.txt"))
    NbItems = ListBox1.Items.Count
    NbAleatoire = Aleatoire.Next(NbItems)
    Mot.MotSecret = ListBox1.Items(NbAleatoire)

    Return Mot
End Function

End Class
4

1 回答 1

0

由于您没有为它设置任何值,因此Mot.LettreDecouverte可以Nothing不初始化。

这可能会导致错误发生在代码Mot.LettreDecouverte(j) = Mot.MotSecret(j)行和If Mot.LettreDecouverte(j).Equals("") Then...代码行中。

更改您的ButtonA_Click事件处理程序代码如下:

i = i + 1
ActiveControl.Visible = False
PictureBox1.Image = ImageList1.Images(i - 1)
Dim j As Integer = 0

If Mot.LettreDecouverte Is Nothing OrElse Mot.LettreDecouverte.Length < Mot.MotSecret.Length Then
        Mot.LettreDecouverte = Space(Mot.MotSecret.Length) '* initiate it by enough number of space chars
End If

For j = 0 To Mot.MotSecret.Length - 1
    If ActiveControl.Text = Mot.MotSecret(j) Then
        Mot.LettreDecouverte(j) = Mot.MotSecret(j)
    End If
Next j

Label1.Text = ""
For j = 0 To Mot.MotSecret.Length - 1
    Label1.Text = Label1.Text + " "
    If Mot.LettreDecouverte(j).Equals(" "c) Then '*Note: i use space char, not empty string
        Label1.Text = Label1.Text + "_"
    Else
        Label1.Text = Label1.Text + Mot.LettreDecouverte(j)
    End If
Next j

请注意,的值Mot.LettreDecouverte(j)始终是char,并且 char 不能是您输入的空字符串Mot.LettreDeciouverte(j).Equals("")。我用一个空格字符数组启动 LettreDeciouverte,这样我们就可以检查它的元素与" "cchar 的比较。

于 2013-09-24T07:57:50.833 回答