需要帮助创建一个简单的数学测验(Visual Basic)

时间:2014-09-18 18:56:37

标签: vb.net visual-studio-2010 visual-studio

我是一个相当新的程序员,我正在寻找当前任务的帮助。我试图创建一个生成2个随机数和一个随机运算符的程序,然后让用户输入他们的答案。我想我已经完成了这一部分。

然而,当我尝试让程序显示正确的答案时,它只是显示问题并且没有用正确的答案回答问题。

非常感谢任何帮助!

Module Module1

Sub Main()
    Randomize()
    Dim RandomNum As New Random

    Dim Number1 As Integer
    Dim Number2 As Integer
    Dim Operation As Integer
    Dim Output As String
    Dim Answer As String


    Number1 = RandomNum.Next(1, 20)
    Number2 = RandomNum.Next(1, 20)
    Operation = RandomNum.Next(1, 4)


    If (Operation = 1) Then
        Output = " + "
    ElseIf (Operation = 2) Then
        Output = " - "
    ElseIf (Operation = 3) Then
        Output = " * "
    Else
        Output = " / "
    End If


    Console.WriteLine("What is " & Number1 & Output & Number2 & "?")
    Answer = Console.ReadLine()

    Console.WriteLine("Your answer is " & Answer)
    Console.WriteLine("The correct answer is " & Number1 & Output & Number2) <------ Here is where the problem is

    Console.ReadLine()

End Sub

End Module

2 个答案:

答案 0 :(得分:2)

您需要添加计算正确答案的内容:

Dim correctAnswer as Single

然后更改为以下内容:

If (Operation = 1) Then
    Output = " + "
    correctAnswer = Number1 + Number2
ElseIf (Operation = 2) Then
    Output = " - "
    correctAnswer = Number1 - Number2
ElseIf (Operation = 3) Then
    Output = " * "
    correctAnswer = Number1 * Number2
Else
    Output = " / "
    correctAnswer = Number1 / Number2
End If

最后,改为:

Console.WriteLine("The correct answer is " & correctAnswer)

答案 1 :(得分:1)

我还建议您使用Select语句而不是连续的If-Else语句。 像这样:

Dim Answer as Decimal

Select Case (Operation)
    Case 1
        Output = " + "
        Answer = Number1 + Number2
    Case 2
        Output = " - "
        Answer = Number1 - Number2
    Case 3
        Output = " * "
        Answer = Number1 * Number2
    Case Else
        Output = " / "
        Answer = Number1 / Number2
End Select