我在Vb上打了21场比赛。游戏的目的是让用户和计算机战斗。计算机生成一个1-3的随机数,然后输出。然后这取消了总数21.然后客户做同样的事情,直到最终的数字为0.但是这个游戏不会停在0并且继续进行。计算机和用户都应该使用相同的21变量,我也不知道如何使其工作。所以
这是我的代码:
Sub Main()
Console.WriteLine("In this game the aim is to be the last to get to 0 ")
Dim originalnum, playerturn As Integer
originalnum = 21
While playerturn <= 21
Randomize()
Console.WriteLine("Please enter your guess from 1 to 3. Press enter after guess.")
playerturn = Console.ReadLine()
If playerturn > 3 Then
Console.WriteLine("This is two high. Pick between 1 and 3.")
End If
originalnum = originalnum - playerturn
Console.WriteLine("Your total is, {0}", originalnum)
If playerturn >= 21 Then
Console.WriteLine("You lost, the computer won")
End If
Dim random As New Random
Dim computer As Integer
computer = random.Next(0, 4)
Console.WriteLine("The computer's go is, {0}", computer)
originalnum = originalnum - computer
Console.WriteLine("The computer's total is, {0}", originalnum)
If computer >= 21 Then
Console.WriteLine("The computer lost, You Won !")
End If
End While
Console.ReadLine()
End Sub
答案 0 :(得分:0)
Sub Main()
Console.WriteLine("In this game the aim is to be the last to get to 0 ")
Dim originalnum, playerturn As Integer
Dim PlayerNumber, ComputerNumber As Integer
originalnum = 21
While playerturn <= 21
Randomize()
Console.WriteLine("Please enter your guess from 1 to 3. Press enter after guess.")
playerturn = Console.ReadLine()
If playerturn > 3 Then
Console.WriteLine("This is two high. Pick between 1 and 3.")
End If
originalnum = originalnum - playerturn
PlayerNumber = originalnum
Console.WriteLine("Your total is, {0}", originalnum)
If playerturn >= 21 Then
Console.WriteLine("You lost, the computer won")
End If
Dim random As New Random
Dim computer As Integer
computer = random.Next(0, 4)
Console.WriteLine("The computer's go is, {0}", computer)
originalnum = originalnum - computer
ComputerNumber = originalnum
Console.WriteLine("The computer's total is, {0}", originalnum)
If computer >= 21 Then
Console.WriteLine("The computer lost, You Won !")
End If
If PlayerNumber <= 0 Then
Console.WriteLine("The computer lost, You Won !")
End If
If ComputerNumber <= 0 Then
Console.WriteLine("The computer Won, You Lost !")
End If
End While
Console.ReadLine()
End Sub
答案 1 :(得分:0)
你的规则有点矛盾。首先你说游戏应该从21开始到0结束,但是你说游戏应该停在21。我假设它应该从21开始到0结束。
你的逻辑有一些问题。游戏永远不会结束,因为循环中的条件是变量playerturn
必须小于21才能继续循环。 playerturn
变量用于保持玩家的猜测,该值应始终在1到3之间(您正在检查此但不会强制玩家重新输入他们的猜测。我可以输入100,获取消息太高了,游戏将继续使用我的猜测100)。
您正在从originalnum
变量中减去播放器和计算机的猜测,但从不检查它的值。我假设一旦达到0,无论谁得到它就有一个人输了,对吗?
下面的内容应该有效。我修改了检查playerturn >= 21
和computer >= 21
的条件,检查originalnumber
是否小于或等于0.我还修改了你得到的位并检查用户猜测.. .it将循环,直到收到有效的输入。
请注意,您的代码仍存在一些问题,请尝试转为Option Strict On
。你也应该测试你的玩家猜测,这样如果他们输入一个字母或一个数字以外的任何字符,它就不会引起异常。
Sub Main()
Console.WriteLine("In this game the aim is to be the last to get to 0 ")
Dim originalnum As Integer = 21
While originalnum > 0
Dim playerturn As Integer = 0, computer As Integer = 0
Randomize()
'player input
playerturn = CInt(Console.ReadLine())
While playerturn <= 0 OrElse playerturn > 3
Console.WriteLine("Please enter your guess from 1 to 3. Press enter after guess.")
playerturn = CInt(Console.ReadLine())
If playerturn <= 0 Then
Console.WriteLine("This is too low. Pick between 1 and 3.")
ElseIf playerturn > 3 Then
Console.WriteLine("This is too high. Pick between 1 and 3.")
End If
End While
'check player answer
originalnum = originalnum - playerturn
Console.WriteLine("Your total is, {0}", originalnum)
If originalnum <= 0 Then
Console.WriteLine("You lost, the computer won")
Exit While
End If
'computer input
Dim random As New Random
computer = random.Next(0, 4)
Console.WriteLine("The computer's go is, {0}", computer)
'check computer answer
originalnum = originalnum - computer
Console.WriteLine("The computer's total is, {0}", originalnum)
If originalnum <= 0 Then
Console.WriteLine("The computer lost, You Won !")
End If
End While
Console.ReadLine()
End Sub