【发布时间】:2014-03-11 03:57:11
【问题描述】:
我正在创建一个石头剪刀布游戏。我需要代码循环,直到玩家选择退出。我怎样才能做到这一点。
Imports System.Random
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As New Random()
Dim b As Integer = a.Next(1, 3)
Dim played As Integer = 0
Dim won As Integer = 0
Dim lost As Integer = 0
Dim draw As Integer = 0
Dim percent As Single = 0.0
If b = 1 Then
Label3.Text = "Rock"
Label3.Visible = True
Select Case ComboBox1.SelectedIndex
Case ComboBox1.SelectedIndex = 1
MsgBox("Draw.")
draw += 1
Case ComboBox1.SelectedIndex = 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case ComboBox1.SelectedIndex = 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
ElseIf b = 2 Then
Label3.Text = "Paper"
Label3.Visible = True
Select Case ComboBox1.SelectedIndex
Case ComboBox1.SelectedIndex = 1
MsgBox("Paper Covers Rock. You lose.")
lost += 1
Case ComboBox1.SelectedIndex = 2
MsgBox("Draw.")
draw += 1
Case ComboBox1.SelectedIndex = 3
MsgBox("Scissors Cuts Paper. You win!")
won += 1
End Select
ElseIf b = 3 Then
Label3.Text = "Scissors"
Label3.Visible = True
Select Case ComboBox1.SelectedIndex
Case ComboBox1.SelectedIndex = 1
MsgBox("Rock Crushes Scissors. You win!")
won += 1
Case ComboBox1.SelectedIndex = 2
MsgBox("Scissors Cuts Paper. You lose.")
lost += 1
Case ComboBox1.SelectedIndex = 3
MsgBox("Draw.")
draw += 1
End Select
End If
played += 1
percent = won / played
PlayedText.Text = played.ToString
WonText.Text = won.ToString
LostText.Text = lost.ToString
DrawText.Text = draw.ToString
PercentText.Text = percent.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
【问题讨论】:
-
什么需要循环?从您的代码中,用户不能一直单击
Button1吗?您的所有“玩家统计信息”变量都在按钮处理程序中声明,因此它们将在每次点击时重置。 -
我是这么认为的,但是当我第二次按下它时,play 保持 1 并重置其他并在相应的框中添加 1 并根据一场比赛计算百分比。所以,代码可以工作,但它不能识别出第二场比赛,它只是假设它是 1。我想循环游戏本身。