条件|语句
本文适合读者主要是入门读者和对VB入门教学有兴趣的朋友。条件判断是计算机编程中完成主要逻辑结构的流程控制语句。在VB中提供了IF 条件表达式1 then [代码部分]elseIf 条件表达式2 then [代码部分]else [代码部分]end IF
的控制流程来控制程序的执行结构。条件表达式决定了执行条件的判断。这里我们来举个例子说明该控制流程的工作方法。 本例通过用VB实现一个猜拳程序来分析条件判断语句。首先我们先来分析一下基本的猜拳规则:1. 猜拳包括三个基本的拳(石头,剪刀,布)2. 胜负规则:石头>剪刀 剪刀>布 布>石头然后我们来考虑猜拳游戏的实现,首先猜拳是由选手和电脑之间的比赛进行的,电脑的猜拳我们通过随机数来生成vb的随机输函数rnd函数可以生成0~1之间的随机数我们通过Int(rnd()*3)+1来实现生成【1~3】的随机数,表示(石头,剪刀,布),而选手选择的拳记录在变量中playerGuess,然后我们再来编写规则来实现。对于编写规则我们就可以运用到上面所介绍的条件判断的流程控制了。通过条件判断的流程控制我们可以做如下比较,如果电脑出(石头,剪刀,布)的一种,选手出的(石头,剪刀,布)的一种,那么就有9种组合规则,因此这里就需要嵌套的条件判断。 Dim guess As String GuessTimer.Enabled = False guess = randGuess(Int(Rnd() * 3) + 1) RandLabel.Caption = guess If guess = "石头" Then If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'石头' 双方平" Draw = Draw + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'布' 电脑出'石头' 玩家赢" Win = Win + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'剪刀' 电脑出'石头' 电脑赢" Own = Own + 1 End If ElseIf guess = "剪刀" Then If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'剪刀' 玩家赢" Win = Win + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'剪刀' 电脑出'剪刀' 双方平" Draw = Draw + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'布' 电脑出'剪刀' 电脑赢" Own = Own + 1 End If Else 'guess="布" If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'布' 电脑赢" Own = Own + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'剪刀' 电脑出'布' 玩家赢" Win = Win + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'布' 电脑出'布' 双方平" Draw = Draw + 1 End If End If ResultLabel.Caption = "赢:" & Win & "场" & "平:" & Draw & "场" & "负:" & Own & "场"
同时我们统计赢,平,负的场数。
整个程序设计:
程序界面设计
程序源代码:
Private playerGuess As StringPrivate randGuess() As StringPrivate Win As SinglePrivate Own As SinglePrivate Draw As Single
Private Sub ClothCommand_Click() playerGuess = "Cloth" GuessTimer.Enabled = True TitleLabel.Caption = "玩家准备出'布'"End Sub
Private Sub ForfexCommand_Click() playerGuess = "Forfex" GuessTimer.Enabled = True TitleLabel.Caption = "玩家准备出'剪刀'"End Sub
Private Sub Form_Load() ReDim randGuess(3) randGuess(1) = "石头" randGuess(2) = "剪刀" randGuess(3) = "布" ResultLabel.Caption = "赢:" & Win & "场" & "平:" & Draw & "场" & "负:" & Own & "场" Dim sum As Single sum = 0 For i = 1 To 12 sum = sum + i Next i MsgBox sumEnd Sub
Private Sub GuessCommand_Click() Dim guess As String GuessTimer.Enabled = False guess = randGuess(Int(Rnd() * 3) + 1) RandLabel.Caption = guess If guess = "石头" Then If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'石头' 双方平" Draw = Draw + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'布' 电脑出'石头' 玩家赢" Win = Win + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'剪刀' 电脑出'石头' 电脑赢" Own = Own + 1 End If ElseIf guess = "剪刀" Then If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'剪刀' 玩家赢" Win = Win + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'剪刀' 电脑出'剪刀' 双方平" Draw = Draw + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'布' 电脑出'剪刀' 电脑赢" Own = Own + 1 End If Else 'guess="布" If playerGuess = "Rock" Then TitleLabel.Caption = "玩家出'石头' 电脑出'布' 电脑赢" Own = Own + 1 ElseIf playerGuess = "Forfex" Then TitleLabel.Caption = "玩家出'剪刀' 电脑出'布' 玩家赢" Win = Win + 1 Else 'playerGuess="Cloth" TitleLabel.Caption = "玩家出'布' 电脑出'布' 双方平" Draw = Draw + 1 End If End If ResultLabel.Caption = "赢:" & Win & "场" & "平:" & Draw & "场" & "负:" & Own & "场"End Sub
Private Sub GuessTimer_Timer() RandLabel.Caption = randGuess(Int(Rnd() * 3) + 1)End Sub
Private Sub RockCommand_Click() playerGuess = "Rock" GuessTimer.Enabled = True TitleLabel.Caption = "玩家准备出'石头'"End Sub
总结:
本程序是一个简单易懂的VB入门程序,通过该程序使刚起步的朋友在兴趣中了解程序的基本逻辑结构同时抛开了C的机器复杂性。