【问题标题】:I want to make a maths quiz on vb.net that uses bracket questions我想在 vb.net 上做一个使用括号问题的数学测验
【发布时间】:2020-12-02 01:40:15
【问题描述】:

所以我现在使用了一些视觉基础知识 (vb.net) 并了解了一些东西。现在我想做一个数学测验,当我单击一个按钮时,它会将我带到一个新表格并开始测验。当测验开始时,我想要它,所以它给用户随机数,用户需要在文本框中回答它,如果正确,它会转到下一个问题(基本,我应该能够做到)。重要 - 我的问题是,有一个名为 BODMAS (Bracket.Order.Division.Multiply.Add.Subtract) 的数学规则,我想将此规则添加到我的编码中,而不是进行常规的简单数学运算...

示例问题是 2 x (2+3) - 1 = ?

  1. 2 x 5 - 1 = ?
  2. 10 - 1 = ?
  3. 9 = 9 人在文本框中写下答案并移至下一个类似问题

这是我第一次使用它,但我想写得深入一些,以便人们能够理解。如果您找到解释我正在寻找的内容的视频,或者如果有人有我可以下载的类似代码的文件,请帮助我!

【问题讨论】:

    标签: vb.net visual-studio math coding-style vb.net-2010


    【解决方案1】:

    基本上,你需要确定你使用的数字的范围,然后在'*'、'/'、'+'、'-'之间随机匹配。然后随机插入括号。

    Private codeStr As String
    Private Function GenerateMathsQuiz() As String
        Dim r As Random = New Random()
        Dim builder As StringBuilder = New StringBuilder()
        'The maximum number of operations is five, and you can increase the number [5] to increase the difficulty
        Dim numOfOperand As Integer = r.[Next](1, 5)
        Dim numofBrackets As Integer = r.[Next](0, 2)
        Dim randomNumber As Integer
    
        For i As Integer = 0 To numOfOperand - 1
            'All numbers will be random between 1 and 10
            randomNumber = r.[Next](1, 10)
            builder.Append(randomNumber)
            Dim randomOperand As Integer = r.[Next](1, 4)
            Dim operand As String = Nothing
    
            Select Case randomOperand
                Case 1
                    operand = "+"
                Case 2
                    operand = "-"
                Case 3
                    operand = "*"
                Case 4
                    operand = "/"
            End Select
    
            builder.Append(operand)
        Next
    
        randomNumber = r.[Next](1, 10)
        builder.Append(randomNumber)
    
        If numofBrackets = 1 Then
            codeStr = InsertBrackets(builder.ToString())
        Else
            codeStr = builder.ToString()
        End If
    
        Return codeStr
    End Function
    Public Function InsertBrackets(ByVal source As String) As String
        Dim rx As Regex = New Regex("\d+", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        Dim matches As MatchCollection = rx.Matches(source)
        Dim count As Integer = matches.Count
        Dim r As Random = New Random()
        Dim numIndexFirst As Integer = r.[Next](0, count - 2)
        Dim numIndexLast As Integer = r.[Next](1, count - 1)
    
        While numIndexFirst >= numIndexLast
            numIndexLast = r.[Next](1, count - 1)
        End While
    
        Dim result As String = source.Insert(matches(numIndexFirst).Index, "(")
        result = result.Insert(matches(numIndexLast).Index + matches(numIndexLast).Length + 1, ")")
        Return result
    End Function
    

    完成此操作后,您将获得一个数学测验,然后您需要知道如何在运行时编译和运行代码。

    Private Function GetResult(ByVal str As String) As String
        Dim sb As StringBuilder = New StringBuilder("")
        sb.Append("Namespace calculator" & vbCrLf)
        sb.Append("Class calculate " & vbCrLf)
        sb.Append("Public Function Main() As Integer " & vbCrLf)
        sb.Append("Return " & str & vbCrLf)
        sb.Append("End Function " & vbCrLf)
        sb.Append("End Class " & vbCrLf)
        sb.Append("End Namespace" & vbCrLf)
        Dim CompilerParams As CompilerParameters = New CompilerParameters()
        CompilerParams.GenerateInMemory = True
        CompilerParams.TreatWarningsAsErrors = False
        CompilerParams.GenerateExecutable = False
        CompilerParams.CompilerOptions = "/optimize"
        Dim references As String() = {"System.dll"}
        CompilerParams.ReferencedAssemblies.AddRange(references)
        Dim provider As VBCodeProvider = New VBCodeProvider()
        Dim compile As CompilerResults = provider.CompileAssemblyFromSource(CompilerParams, sb.ToString())
    
        If compile.Errors.HasErrors Then
            Dim text As String = "Compile error: "
    
            For Each ce As CompilerError In compile.Errors
                text += "rn" & ce.ToString()
            Next
    
            Throw New Exception(text)
        End If
    
        Dim Instance = compile.CompiledAssembly.CreateInstance("calculator.calculate")
        Dim type = Instance.GetType
        Dim methodInfo = type.GetMethod("Main")
        Return methodInfo.Invoke(Instance, Nothing).ToString()
    
    End Function
    

    最后,您可以使用以下方法:

    Private Sub GetMathQuizBtn_Click(sender As Object, e As EventArgs) Handles GetMathQuizBtn.Click
        Label1.Text = GenerateMathsQuiz()
    End Sub
    Private Sub ResultBtn_Click(sender As Object, e As EventArgs) Handles ResultBtn.Click
        If TextBox1.Text = GetResult(Label1.Text) Then
            MessageBox.Show("bingo!")
            TextBox1.Text = ""
            Label1.Text = GenerateMathsQuiz()
        Else
            MessageBox.Show("result is wrong")
        End If
    End Sub
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2011-08-16
      • 2016-09-28
      • 1970-01-01
      • 2011-07-25
      • 2010-09-21
      相关资源
      最近更新 更多