Reputation: 1
This is my code so far
Module Module1
Sub Main()
Randomize()
Dim number1 As Integer
Dim number2 As Integer
Dim answer As Integer
Dim userAnswer As Integer
Dim name As String
Console.WriteLine("Hello! Welcome to your Maths Quiz! Please enter your name >")
name = Console.ReadLine
Console.WriteLine("Nice to meet you " + name + ". Lets start the quiz")
Randomize()
number1 = Rnd() * 11 + 1
number2 = Rnd() * 11 + 1
answer = number1 + number2
Try
For i As Integer = 0 To 10
Console.WriteLine("What is " & number1 & " + " & number2 & " = ?")
userAnswer = Console.ReadLine
If userAnswer = answer Then
Console.WriteLine("Correct!")
Else
Console.WriteLine("Incorrect, the answer was " & answer)
End If
Next
Catch ex As InvalidCastException
Console.WriteLine("Oops you have typed in a number, please start over")
End Try
Console.ReadKey()
End Sub
End Module
I need to create a random function to take place of the "+" sign and i have tried many ways but the output comes up weird, i was wondering if you can help, Thanks
Upvotes: 0
Views: 2157
Reputation: 413
If you are going for + - * / then I would do this, if only + - and could go with IIF statments
Create a new random variable operator
op = Int(Rnd() * 4) '0+ 1- 2* 3/
Calculate the anwser with a function
answer= calc(number1, number2, op)
Function calc(n1, n2, op)
If op = 0 Then calc = n1 + n2
If op = 1 Then calc = n1 - n2
If op = 2 Then calc = n1 * n2
If op = 3 Then calc = n1 / n2
End Function
And 1 more function to get the operator sign
Console.WriteLine("What is " & number1 & s_op(op) & number2 & " = ?")
Function s_op(op)
If op = 0 Then s_op = "+"
If op = 1 Then s_op = "-"
If op = 2 Then s_op = "*"
If op = 3 Then s_op = "/"
End Function
Upvotes: 1