2013-04-17 34 views
0

基本上我必须设计一个水果游戏,随机水果将被生成,并且孩子必须猜测水果类型。如果孩子猜对了,他们会收到一条消息说恭喜,如果他们错了,他们会收到一条消息,说“再试一次”。问题与一些Visual Basic编程代码

我已经完成了编程,但是当我输入水果的名字时,我不知道错误的位置。因为即使它是正确的,它也会给出一个消息,说它是错误的。

我有程序代码,当水果是正确的,并与它的消息。

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 
+3

VB.NET与VBA不一样,它们与VBScript或VB6都不相同。你究竟使用哪一个?这些标签中只有一个可以是正确的。 (是的,我知道哪一个,你也应该:-)) –

+1

另外,你的问题是什么? “我不知道该做什么”不是问题。 – Neolisk

+0

让我澄清你正在显示一张带有标签(或标签)的图片,孩子必须在文本框中输入他/她的猜测?你在这里做什么? – Edper

回答

2

您的问题的答案可能是由于套管问题。尝试使用ToLower()并确保您的字符串也是小写字母。

尽管如此,如果您创建了一个名为Fruit的抽象类,然后从该类中派生出不同类型的水果(苹果,草莓等),您可以更有效地编写该代码。然后,您可以创建一个名为ToString()的抽象方法,并将输入与ToString()方法进行比较。这样可以让你不必在代码中混杂一堆“If”。

+0

他们在比较字符串方面苦苦挣扎,你推荐使用抽象基类重构?学习曲线,你说? –

+0

@DavidOsborne - 大声笑很少,是吧?那么你永远不知道,它可能也会帮助其他程序员在这里 – Icemanind

0

只是为了澄清icemanind的说法,您想删除您的字符串的任何案件问题。我可以看到你有草莓(大写字母S)和橙色(没有大写字母),这意味着你没有设置真正的字符串结构。记住,当你检查字符串是否匹配时,他们必须按照你测试的那样输入它。

因此您使用.toUpper或.toLower。这将文本转换从文本到低于或大写的 - 那么你测试的字符串应该是大写所有较低(更低的是容易我猜。)

所以每行应该更像...

If Label1.Text = "1" Andalso TextBox2.Text.ToLower = "banana" Then 

现在你可以在文本框中输入BAnaNa,它将被测试为香蕉。另外注意我已经使用了Andalso - 它对未来的使用有好处。 Andalso应该用于这类测试,因为它只在第一个条件有效时才会检查第二个条件。并检查两个,即使第一个是错误的。不是一件大事,但良好的做法和更好的表现。