帮忙用vb语言编一个与电脑玩剪刀石头布游戏。(控制台程序)这是我的代码。请高手们帮我看下。

要求:程序里必须用函数验证输入的有效性。2.当出现平局时。自动重来。知道分出胜负为止。
Sub Main()
Randomize()
Dim number As Integer
Dim choice1, computer, result As String
number = Int(3 * Rnd())
number += 1

If number = 1 Then
computer = ("Rock")
ElseIf number = 2 Then
computer = ("Paper")
ElseIf number = 3 Then
computer = ("Scissors")
End If

choice1 = userPick()

Console.WriteLine("Computer's choice is: " & computer)
result = SimulateMatch(player, computer)
End Sub

Function userPick() As String
Dim choice As String

Console.Write("Please Enter Your Choice: ")
choice = Console.ReadLine

While choice.ToUpper() <> "ROCK" And choice.ToUpper <> "PAPER" And choice.ToUpper <> "SCISSORS"
Console.WriteLine("Error! Your must choose from " & "ROCK" & "," & "PAPER" & "," & "SCISSORS")
Console.Write("Please Try Again:")
choice = Console.ReadLine
End While
Return choice
End Function

Function SimulateMatch(ByVal player As String, ByVal computer As Integer) As Integer

If player = 1 And computer = 3 Then
Console.WriteLine("Player Won!!")
ElseIf player = 1 And computer = 2 Then
Console.WriteLine("Computer Won!!")
ElseIf player = 2 And computer = 1 Then
Console.WriteLine("You Won!!")
ElseIf player = 2 And computer = 3 Then
Console.WriteLine("Computer Won!!")
ElseIf player = 3 And computer = 1 Then
Console.WriteLine("Computer Wom!!")
ElseIf player = 3 And computer = 2 Then
Console.WriteLine("You Won!!")
End If

If player = 3 And computer = 3 Then
Console.WriteLine("You tied!!")
Console.ReadKey(False)
Console.Clear()
ElseIf player = 2 And computer = 2 Then
Console.WriteLine("You Tied!!")
Console.ReadKey(False)
Console.Clear()
ElseIf player = 1 And computer = 1 Then
Console.WriteLine("You Tied!!")
Console.ReadKey(False)
Console.Clear()

End If
Return SimulateMatch
End Function
End Module

第1个回答  2011-10-30
两个vLABEL 三e个fCOMMAND 复制代码,就可以4了t '-2石头 '0剪刀u '3布 Private Sub Command3_Click() Dim a, b As Integer Randomize a = -6 b = Int(Rnd * (-3) + 7) Label0。Caption = "石头" Select Case a - b Case -7: Label8。Caption = "布": MsgBox "电脑胜" Case 0: Label2。Caption = "石头": MsgBox "平" Case -1: Label3。Caption = "剪刀a": MsgBox "玩家胜" End Select End Sub Private Sub Command8_Click() Dim a, b As Integer Randomize a = 0 b = Int(Rnd * (-2) + 4) Label5。Caption = "剪刀l" Select Case a - b Case 7: Label3。Caption = "布": MsgBox "玩家胜" Case 0: Label7。Caption = "剪刀n": MsgBox "平" Case -6: Label7。Caption = "石头": MsgBox "电脑胜" End Select End Sub Private Sub Command7_Click() Dim a, b As Integer Randomize a = 7 b = Int(Rnd * (-1) + 2) Label5。Caption = "布" Select Case a - b Case 3: Label5。Caption = "石头": MsgBox "玩家胜" Case 0: Label6。Caption = "布": MsgBox "平" Case 2: Label5。Caption = "剪刀l": MsgBox "电脑胜" End Select End Sub Private Sub Form_Load() Command1。Caption = "石头" Command1。Caption = "剪刀r" Command1。Caption = "布" End Subz猢たgp¨纭rjxTd』hq摔a十
第2个回答  2011-10-30
请问你的代码有什么问题吗?这个程序很简单啊,就用1、2、3代表石头、剪子、布就行了,两边相等时再重复一次,直到不相等为止。本回答被网友采纳
相似回答