Vb.net 尝试做一个反复问随机问题的数学游戏,如果你做对了,分数会上升一分
尝试做一个反复问随机问题的数学游戏,如果你做对了,分数会上升一分。 这是到目前为止我的代码Vb.net 尝试做一个反复问随机问题的数学游戏,如果你做对了,分数会上升一分,vb.net,Vb.net,尝试做一个反复问随机问题的数学游戏,如果你做对了,分数会上升一分。 这是到目前为止我的代码 Sub Main() Randomize() Dim RandomNum As New Random Dim num1, num2, operation, score As Integer Dim output As String Dim correctAnswer As Single num1 = Ran
Sub Main()
Randomize()
Dim RandomNum As New Random
Dim num1, num2, operation, score As Integer
Dim output As String
Dim correctAnswer As Single
num1 = RandomNum.Next(1, 5)
num2 = RandomNum.Next(1, 5)
operation = RandomNum.Next(1, 4)
If (operation = 1) Then
output = " + "
correctAnswer = num1 + num2
ElseIf (operation = 2) Then
output = " - "
correctAnswer = num1 - num2
ElseIf (operation = 3) Then
output = " * "
correctAnswer = num1 * num2
Else
output = " / "
correctAnswer = num1 / num2
End If
While True
Console.WriteLine("What is " & num1 & output & num2 & "?")
If Console.ReadLine = correctAnswer Then
score = score + 1
Console.WriteLine("The correct answer is " & correctAnswer)
Console.WriteLine("Your score is " & score)
Console.ReadLine()
Else
score = score - 1
Console.WriteLine("The correct answer is " & correctAnswer)
Console.WriteLine("Your score is " & score)
Console.ReadLine()
End If
End While
End Sub
在用户回答第一个问题后,我似乎无法获得新问题。您需要增加已完成的操作量,并在
While
循环中移动您的操作条件逻辑。大概是这样的:
Sub Main()
Randomize()
Dim RandomNum As New Random
Dim num1, num2, operation, score As Integer
Dim output As String
Dim correctAnswer As Single
operation = 1
While True
num1 = RandomNum.Next(1, 5)
num2 = RandomNum.Next(1, 5)
If (operation = 1) Then
output = " + "
correctAnswer = num1 + num2
ElseIf (operation = 2) Then
output = " - "
correctAnswer = num1 - num2
ElseIf (operation = 3) Then
output = " * "
correctAnswer = num1 * num2
Else
output = " / "
correctAnswer = num1 / num2
End If
Console.WriteLine("What is " & num1 & output & num2 & "?")
If Console.ReadLine = correctAnswer Then
score = score + 1
Console.WriteLine("The correct answer is " & correctAnswer)
Console.WriteLine("Your score is " & score)
Console.ReadLine()
Else
score = score - 1
Console.WriteLine("The correct answer is " & correctAnswer)
Console.WriteLine("Your score is " & score)
Console.ReadLine()
End If
operation = operation + 1
If operation = 5 Then ' Basically after all operations have been executed
Console.WriteLine("You have finished the Math Test!")
Console.WriteLine("Your final score is " & score)
Console.ReadLine()
Environment.Exit(0)
End If
End While
End Sub
一切正常。请注意,现在这些数字每次都是随机的,以使其更好(我猜这就是您希望发生的事情)。第一个操作也不再是随机的。如果您尝试单步执行代码,您将看到while循环不会重新计算随机生成器以获得另一个问题。您需要将while true语句移到提取下一个随机问题的位置。您需要将计算下一个答案的所有逻辑放在循环中调用的子循环中。此外,还需要使所涉及的变量对新的子循环和主循环都可见。