【问题标题】:VB.net basic error with String <-> Char ConversionVB.net 字符串 <-> 字符转换的基本错误
【发布时间】:2013-09-24 00:57:25
【问题描述】:

我正在编写一个小游戏来练习自己,但我遇到了一个我无法修复的错误: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

【问题讨论】:

  • 实际的错误信息和行号是什么?
  • 为什么要根据 Char 数组而不是字符串来比较单词?法语命名使理解您的算法有点困难。 Ce Pas Grave 但 mon Fracais Ce Pas Esceptionel。如果您可以在 github 或 gist.github.com 上发布完整的代码,我愿意更深入地了解一下。好机会。
  • 他们说错误在这一行: If Mot.LettreDecouverte(j).Equals("") Then
  • MotSecret() 在你的结构中应该是一个字符串而不是一个字符...

标签: vb.net string variables char type-conversion


【解决方案1】:

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,这样我们就可以检查它的元素与" "c char 的比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 2014-02-27
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2015-03-22
    相关资源
    最近更新 更多