This is an assignment. I have it all pretty much done, but I am stuck on a particular spot. As it is I can call this assignment done, but I'm more of a perfectionist. I am in a basic class, but understand the basics.
A little background on my program. It is a lottery. A user will input 5 numbers, I will generate 5 random numbers, compare the two, if the person gets 3 or more correct a message will display stating they won a prize. My number range is from 0-9. Here is my current code copied and pasted from VB. It all works and compiles just fine. After the code I will post my issue..
Here's the issue I am not at terms with. When a user inputs a number other than the range I selected I would like it to loop back. I tried what I thought was a solution, but somehow I ended up in an infinite loop. I was originally going to let it be, but that's not the type of person I am. I tried adding..
...but that string of code throws me into an infinite loop. Even if I put a number in the range I specified I go into an infinite loop. Any ideas would be greatly appreciated!
-James
A little background on my program. It is a lottery. A user will input 5 numbers, I will generate 5 random numbers, compare the two, if the person gets 3 or more correct a message will display stating they won a prize. My number range is from 0-9. Here is my current code copied and pasted from VB. It all works and compiles just fine. After the code I will post my issue..
Code:
Module Module1
Sub Main()
'declare variables
Dim num1 As Integer
Dim total As Integer
Dim again As Char = "NO VALUE"
'calling modules
welcomeMessage()
inputNumbers(num1, total)
displayTotalCorrect(total)
displayPrize(total)
playAgain(again)
End Sub
Sub welcomeMessage()
'welcome message
Console.WriteLine("Welcome to the Lottery!")
Console.WriteLine("You will enter 5 single digit numbers (0-9).")
Console.WriteLine("If at least 3 match, you're a winner!!")
Console.WriteLine()
End Sub
Sub inputNumbers(ByRef num1 As Integer, ByRef total As Integer)
'declaring variables
Dim counter As Integer = 5
Dim randomNumber As New Random()
'setting total to 0 to keep a count for correct number guesses
total = 0
'starting a loop to get all 5 guesses from the user
For counter = 1 To 5
Console.Write("What is your number: ")
num1 = Console.ReadLine()
Console.WriteLine()
'generating a random number in the range of 0-9 only
Dim num2 = randomNumber.Next(0, 9)
Console.WriteLine("Your number: {0} My number:{1}", num1, num2, "")
Console.WriteLine()
Console.WriteLine()
'if the user correctly guessed the number, the total goes up by 1
If num1 = num2 Then
total = total + 1
End If
Next
End Sub
Sub displayTotalCorrect(ByVal total As Integer)
'this will display how many correct guesser the user got
Console.WriteLine()
Console.WriteLine()
If total = 0 Then
Console.WriteLine("You did not guess any correct numbers.")
Console.WriteLine()
ElseIf total = 1 Then
Console.WriteLine("You correctly guessed {0} number!", total, "")
Console.WriteLine()
ElseIf total > 1 Then
Console.WriteLine("You correcty guessed {0} numbers!", total, "")
Console.WriteLine()
End If
End Sub
Sub displayPrize(ByVal total As Integer)
'if the total guesser is greater than or equal to 3, the proper prize will be displayed
If total = 3 Then
Console.WriteLine("You win $100!")
Console.WriteLine()
ElseIf total = 4 Then
Console.WriteLine("You win $800!!")
Console.WriteLine()
ElseIf total = 5 Then
Console.WriteLine("You win the Jackpot of $5,000!!!")
Console.WriteLine()
End If
End Sub
Sub playAgain(ByRef again As Char)
'exit message/loop if the user would like to play the game again
Console.WriteLine("Thank you for playing the Lottery! ")
Console.WriteLine()
Console.WriteLine("Would you like to play again? y/n")
Console.WriteLine()
again = Console.ReadLine()
If again = "y" Or again = "Y" Then
Console.WriteLine()
Console.WriteLine()
Dim num1 As Integer
Dim total As Integer
inputNumbers(num1, total)
displayTotalCorrect(total)
displayPrize(total)
playAgain(again)
End If
End Sub
End Module
Here's the issue I am not at terms with. When a user inputs a number other than the range I selected I would like it to loop back. I tried what I thought was a solution, but somehow I ended up in an infinite loop. I was originally going to let it be, but that's not the type of person I am. I tried adding..
Code:
If num1 <> 0 Or num1 <> 1 Or num1 <> 2 Or num1 <> 3 Or num1 <> 4 Or num1 <> 5 Or num1 <> 6 Or num1 <> 7 Or num1 <> 8 Or num1 <> 9 Then
inputNumbers(num1, total)
...but that string of code throws me into an infinite loop. Even if I put a number in the range I specified I go into an infinite loop. Any ideas would be greatly appreciated!
-James