基本上我必须设计一个水果游戏,其中会生成随机水果,孩子必须猜测水果类型。如果孩子猜对了,他们会收到一条消息说恭喜,如果他们弄错了,他们会收到一条消息说“再试一次”#34;。
我已完成编程,但在输入水果名称时,我不知道自己哪里出错了。因为即使它是对的,它也会给出一条消息,说它是错误的。
我有水果是正确的程序代码和带有它的信息。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Visible = True ' The tools that will need to be hide when the check button is cliked
Button2.Visible = False
PictureBox1.Visible = False
TextBox1.Visible = False
If Label1.Text = "1" And TextBox2.Text = "Banana" Then ' If both are true then the following result is true
PictureBox2.Image = My.Resources.Well_done 'my.resources.name 'The well_done picture that appears for the correct
PictureBox2.Visible = True '- answer and at the same time it has to be visble
Label2.Text = "Congrats! " & TextBox2.Text & "! Correct answer!" 'The msg which appears for the correct answer
Label2.Visible = True
Me.BackColor = Color.Yellow ' The background colour of the form
ElseIf Label1.Text = "2" And TextBox1.Text = "apple" Then 'Similary for apple banana and other fruits
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Green
ElseIf Label1.Text = "3" And TextBox1.Text = "orange" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Orange
ElseIf Label1.Text = "4" And TextBox1.Text = "Strawberry" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.IndianRed
ElseIf Label1.Text = "5" And TextBox1.Text = "Grapes" Then
PictureBox2.Image = My.Resources.Well_done
PictureBox2.Visible = True
Label2.Text = "Congrats " & TextBox2.Text & "! Correct answer!"
Label2.Visible = True
Me.BackColor = Color.Green
答案 0 :(得分:2)
你的问题的答案可能是因为套管问题。尝试使用ToLower()并确保你的字符串也是小写的。
作为旁注,如果您创建了一个名为Fruit的抽象类,然后从该类中派生出不同类型的水果(苹果,草莓等),您可以更有效地编写它。然后你可以创建一个名为ToString()的抽象方法,并将输入与ToString()方法进行比较。这将使您免于在代码中使用“If”的垃圾。
答案 1 :(得分:0)
为了澄清icemanind所说的内容,你想要删除你的字符串的任何案例问题。我可以看到你有草莓(大写字母S)和橙色(没有资本),这意味着你没有设置真正的字符串结构。请记住,当您检查字符串是否匹配时,他们必须在测试时完全键入它。
所以你使用.toUpper或.toLower。这会将文本框中的文本转换为小写或大写 - 然后您测试的字符串应该全部低于大写(更低,我猜更容易。)
所以每一行都应该更像......
If Label1.Text = "1" Andalso TextBox2.Text.ToLower = "banana" Then
现在您可以在文本框中键入BAnaNa,它将作为香蕉进行测试。另外请注意我使用了Andalso - 它有利于将来使用。 Andalso应该用于这些类型的测试,因为它只检查第一个条件是否有效。并检查两者,即使第一个是假的。不是一件大事,而是良好的实践和更好的表现。