Need help creating a simple math quiz (Visual Basic)
I am a fairly new programmer and I am looking for some help with my current task. I am trying to create a program that generates 2 random numbers and a random operator and then asks the user for an answer. I think I did this part.
However, when I try to get the program to display the correct answer, it just shows the problem and doesn't answer the question with the correct answer.
Any help is greatly 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
+3
source to share
2 answers
You need to add something that calculates the correct 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)
+2
source to share
May I also assume that you are using a Select statement instead of sequential If-Else statements. Like this:
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
+1
source to share