Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help creating a simple maths quiz (Visual Basic)

I'm a fairly new programmer and I'm looking for some help with my current task. I'm trying to create a program which generates 2 random numbers and a random operator, then ask the user to type in their answer. I think I've got this part done.

However when I try to get the program to display the correct answer, it simply shows the problem and doesn't answer the question with the correct answer.

Any help is much appreciated!

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
like image 907
JeffCottonBWFC Avatar asked Jul 03 '26 02:07

JeffCottonBWFC


1 Answers

You need to add something that calculates the right answer:

Dim correctAnswer as Single

and then change to the following:

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

and finally, change to:

Console.WriteLine("The correct answer is " & correctAnswer)
like image 78
Jonathan M Avatar answered Jul 04 '26 15:07

Jonathan M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!