Variable won't change - vb.net

I'm trying to finish a project for class that calculates the occupancy rate of a hotel using an InputBox object. However, the variables are getting permanently set as the first value entered.
Allow me to clarify what is happening. I click the button to bring up the InputBox I enter a number e.g. 10, then I click ok, then I enter a new number for the second one e.g. 7, but the list box displays both as the first one. Once I enter all the numbers for the Rooms Sold I then continue to enter the values for Rooms Available, but the number form the first loop somehow carried over to the second one and WILL NOT change.
What can I do to make it so the variable is reset for each iteration of the loop. I have it set the variable back to 0 after it has added itself to the total and the list, but it won't change.
And i did do research. The MSDN knowledge base was no help, I could only find one question on here like this and he just forgot to define a string, and I tried Google but to no avail.
Here is my code:
Public Class frmOccupancyRateCalculator
Private Sub frmOccupancyRateCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstRoomsSold.Items.Clear()
lstRoomsAvailable.Items.Clear()
End Sub
Private Sub btnEnterRoomSales_Click(sender As Object, e As EventArgs) Handles btnEnterRoomSales.Click
' Declare the arithimetic and message variables.
Dim intRoomsSold As Integer
Dim intRoomsAvailable As Integer
Dim intTotalRooms As Integer
Dim intTotalRoomsSold As Integer
Dim intTotalRoomsAvailable As Integer
Dim intRoomsSoldNumberOfEntries As Integer = 1
Dim intRoomsAvailableNumberOfEntries As Integer = 1
Dim intNumberOfEntries As Integer = 1
Dim intMaxNumberOfEntries As Integer = 7
Dim decOccupancyRate As Decimal
Dim strRoomsSold As String
Dim strRoomsSoldInputMessage As String = "Enter the number of rooms sold for floor #"
Dim strRoomsSoldInputHeading As String = "Enter Rooms Sold"
Dim strRoomsSoldNormalMessage As String = "Enter the number of rooms sold for floor #"
Dim strRoomsSoldNonNumericError As String = "Error - Non-Numeric value entered. Please enter a whole number of rooms sold for floor #"
Dim strRoomsSoldNegativeError As String = "Error - Negative number entered. Please enter a whole number greater than zero of rooms sold for floor #"
Dim strRoomsSoldDecimalError As String = "Error - Decimal number entered. Plerase enter a whole number of rooms sold for floor #"
Dim strRoomsAvailable As String
Dim strRoomsAvailableInputMessage As String = "Enter the number of rooms available for floor #"
Dim strRoomsAvailableInputHeading As String = "Enter Rooms Available"
Dim strRoomsAvailableNormalMessage As String = "Enter the number of rooms available for floor #"
Dim strRoomsAvailableNonNumericError As String = "Error - Non-Numeric value entered. Please enter a whole number of rooms available for floor #"
Dim strRoomsAvailableNegativeError As String = "Error - Negative number entered. Please enter a whole number greater than zero of rooms available for floor #"
Dim strRoomsAvailableDecimalError As String = "Error - Decimal number entered. Plerase enter a whole number of rooms available for floor #"
Dim strCancelClicked As String = ""
' Define the RoomsSoldInputMessage variables
strRoomsSold = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")
' Loop to iterate until hours of travel are entered for all days of travel
Do Until intRoomsSoldNumberOfEntries > intMaxNumberOfEntries
' Is the input numeric?
If IsNumeric(strRoomsSold) Then
intRoomsSold = Convert.ToDecimal(strRoomsSold)
' Is the input greater or equal to 0?
If intRoomsSold >= 0 Then
' Is the number of rooms sold a whole number?
'If intRoomsSold Mod 1 = 0 Then
intTotalRoomsSold += intRoomsSold
lstRoomsSold.Items.Add(intRoomsSold)
intRoomsSoldNumberOfEntries += 1
intRoomsSold = 0
strRoomsSoldInputMessage = strRoomsSoldNormalMessage
' Display decimal error message
'Else
'strRoomsSoldInputMessage = strRoomsSoldDecimalError
'End If
' Display negative number error message
Else
strRoomsSoldInputMessage = strRoomsSoldNegativeError
End If
' Display non-numeric error message
Else
strRoomsSoldInputMessage = strRoomsSoldNonNumericError
End If
' Is the number of entries less than or equal to the maximum?
If intRoomsSoldNumberOfEntries <= intMaxNumberOfEntries Then
strRoomsSoldInputMessage = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")
End If
Loop
' Define the RoomsAvailableInputMessage variable
strRoomsAvailable = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")
Do Until intRoomsAvailableNumberOfEntries > intMaxNumberOfEntries
' Is the input numeric?
If IsNumeric(strRoomsAvailable) Then
intRoomsAvailable = Convert.ToDecimal(strRoomsAvailable)
' Is the input greater or equal to 0?
If intRoomsAvailable >= 0 Then
' Is the number of rooms sold a whole number?
'If intRoomsAvailable Mod 1 = 0 Then
intTotalRoomsAvailable += intRoomsAvailable
lstRoomsAvailable.Items.Add(intRoomsAvailable)
intRoomsAvailableNumberOfEntries += 1
intRoomsAvailable = 0
strRoomsAvailableInputMessage = strRoomsAvailableNormalMessage
' Is the number of entries equal to the maximum number of entries?
If intRoomsAvailableNumberOfEntries = intMaxNumberOfEntries Then
intNumberOfEntries += 1
End If
' Display decimal error message
'Else
' strRoomsAvailableInputMessage = strRoomsAvailableDecimalError
' End If
' Display negative number error message
Else
strRoomsAvailableInputMessage = strRoomsAvailableNegativeError
End If
' Display non-numeric error message
Else
strRoomsAvailableInputMessage = strRoomsAvailableNonNumericError
End If
' Is the number of entries less than or equal to the maximum?
If intRoomsAvailableNumberOfEntries <= intMaxNumberOfEntries Then
strRoomsAvailableInputMessage = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")
End If
Loop
' Is the number of rooms sold entries greater than 1?
If intNumberOfEntries > 1 Then
' Display result label and totals
intTotalRooms = intTotalRoomsSold + intTotalRoomsAvailable
decOccupancyRate = intTotalRoomsSold / intTotalRooms
lblResult.Visible = True
lblResult.Text = intTotalRooms & vbNewLine & intTotalRoomsSold & vbNewLine & intTotalRoomsAvailable & vbNewLine & vbNewLine & decOccupancyRate.ToString("P")
' Disable the Enter Room Sales button
btnEnterRoomSales.Enabled = False
' Display error message for no values entered
Else
MsgBox("No Rooms Sold/Available value entered")
End If
End Sub
Private Sub mnuClear_Click(sender As Object, e As EventArgs) Handles mnuClear.Click
' This script is executed when the user taps or clicks the Clear menu item.
' It clears the Room Sales and Rooms Available ListBoxes, hides the Result label,
' enables the Enter Room Sales button.
lstRoomsSold.Items.Clear()
lstRoomsAvailable.Items.Clear()
lblResult.Visible = False
btnEnterRoomSales.Enabled = True
End Sub
Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
' This script is executed when the user taps or clicks the Exit menu item.
' The window is closed and the program is terminated.
Close()
End Sub
End Class

Ok I figured it out. I needed to put the strRoomsSold and strRoomsAvailable variable definitions within the loops, then remove the second sequence within the Do Until loop.
' Loop to iterate until Rooms Sold number of Entries is greater than the maximum
Do Until intRoomsSoldNumberOfEntries > intMaxNumberOfEntries
' Define the RoomsSold variable
strRoomsSold = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")
If strRoomsSold = strCancelClicked Then
strRoomsSoldInputMessage = strRoomsAvailableBlankError
Else
' Is the input numeric?
If IsNumeric(strRoomsSold) Then
intRoomsSold = Convert.ToDecimal(strRoomsSold)
' Is the input greater or equal to 0?
If intRoomsSold >= 0 Then
' Is the number of rooms sold a whole number?
If intRoomsSold Mod 1 = 0 Then
intTotalRoomsSold += intRoomsSold
lstRoomsSold.Items.Add(intRoomsSold)
intRoomsSoldNumberOfEntries += 1
strRoomsSoldInputMessage = strRoomsSoldNormalMessage
' Display decimal error message
Else
strRoomsSoldInputMessage = strRoomsSoldDecimalError
End If
' Display negative number error message
Else
strRoomsSoldInputMessage = strRoomsSoldNegativeError
End If
' Display non-numeric error message
Else
strRoomsSoldInputMessage = strRoomsSoldNonNumericError
End If
End If
Loop
Do Until intRoomsAvailableNumberOfEntries > intMaxNumberOfEntries
' Define the RoomsAvailable variable
strRoomsAvailable = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")
' Is the input numeric?
If IsNumeric(strRoomsAvailable) Then
intRoomsAvailable = Convert.ToDecimal(strRoomsAvailable)
' Is the input greater or equal to 0?
If intRoomsAvailable >= 0 Then
' Is the number of rooms sold a whole number?
If intRoomsAvailable Mod 1 = 0 Then
intTotalRoomsAvailable += intRoomsAvailable
lstRoomsAvailable.Items.Add(intRoomsAvailable)
intRoomsAvailableNumberOfEntries += 1
strRoomsAvailableInputMessage = strRoomsAvailableNormalMessage
' Is the number of entries equal to the maximum number of entries?
If intRoomsAvailableNumberOfEntries = intMaxNumberOfEntries Then
intNumberOfEntries += 1
End If
' Display decimal error message
Else
strRoomsAvailableInputMessage = strRoomsAvailableDecimalError
End If
' Display negative number error message
Else
strRoomsAvailableInputMessage = strRoomsAvailableNegativeError
End If
' Display non-numeric error message
Else
strRoomsAvailableInputMessage = strRoomsAvailableNonNumericError
End If
Loop

Related

How to remove the two lowest grades from the listbox before the total grades are calculated?

I am trying to remove the two lowest grades from the listbox before the average and total of the grades is calculated using Visual Basic form application
I know I need to sort the array but I need to sort it after the listbox has been filled and before the average grade and total grade is calculated.
Please ignore the intMaxNumberOfEntries as 3. I will change it to 13 once I get the code working right.
' The btnEnterGrades_Click event accepts and displays up to 13 grades
' and then calculates and displays the average grade for the student
' Declare and initialize variables
Dim strGrade As String
Dim intSizeOfArray As Integer = 3
Dim decGrade(intSizeOfArray) As Decimal
Dim decAverageGrade As Decimal
Dim decTotalGrades As Integer = 0D
Dim strInputMessage As String = "Enter the grade to be averaged #"
Dim strInputHeading As String = "Enter Grades"
Dim strNormalMessage As String = "Enter the grade to be averaged #"
Dim strNonNumericError As String = "Error - Enter a number for the grade to be averaged"
Dim strNegativeError As String = "Error - Enter a positive number for the grade to be averaged"
Dim objWriter As New IO.StreamWriter("grades.txt")
'Declare and initialize loop variables
Dim strCancelClicked As String = ""
Dim intMaxNumberOfEntries As Integer = 3
Dim intNumberOfEntries As Integer = 1
' This loop allows the user to enter up to 13 grades for the student.
' The loop terminates when the user has entered 13 grades or the user
' taps or clicks the Cancel button or the Close button in the InputBox
'"Primimg the Loop" Accept some value and place it into the strGrade
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strGrade = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determining if Grade entered is numeric. If so, add the item to the listbox
Do Until intNumberOfEntries > intMaxNumberOfEntries Or strGrade = strCancelClicked
If IsNumeric(strGrade) Then
decGrade(intSizeOfArray) = Convert.ToDecimal(strGrade)
If decGrade(intSizeOfArray) > 0 Then
'''''''''''''''''''''''''''''''''''''''''''
lstGrades.Items.Add(decGrade(intSizeOfArray))
'''''''''''''''''''''''''''''''''''''''''''
End If
'Accumulator and Counter??
decTotalGrades += decGrade(intSizeOfArray)
intNumberOfEntries += 1 'or IntNumberOfEntries = intNumberOfEntries + 1
strInputMessage = strNormalMessage
Else
strInputMessage = strNegativeError
End If
Else
strInputMessage = strNonNumericError
End If
If intNumberOfEntries <= intMaxNumberOfEntries Then
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strGrade = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If
Loop
'Sort the Array
Array.Sort(decGrade)
'Put in descending order
Array.Reverse(decGrade)
'Drop 2 lowest scores of array
ReDim decGrade(1)
'Calculates and displays average student grade
If intNumberOfEntries > 1 Then
lblFinalAverageText.Visible = True
decAverageGrade = decTotalGrades / (intNumberOfEntries - 1)
lblFinalAverageText.Text = "The Final Average is: " &
decAverageGrade.ToString("F1")
Else
MsgBox("No grade entered")
End If
I want the program to remove the two lowest grades and I wrote code to sort the array and then sort by descending order and then redim to remove the two lowest grades but I realized the program is adding grades to the decTotalGrades before I start the sorting and redim process. I do not know how to rearrange this code for it to work.
You could refresh the list after collecting all grades and sorting/removing items.
'Collect grades.
Dim grades As New List(Of Decimal)
While grades.Count < 13
Dim strGrade As String = InputBox("Enter the grade to be averaged #" &
grades.Count, "Enter Grades")
If String.IsNullOrEmpty(strGrade) Then
Exit While
Else
Dim grade As Decimal
If Decimal.TryParse(strGrade, grade) Then
grades.Add(grade)
lstGrades.Items.Add(grade)
End If
End If
End While
'Perform logic on the complete list of grades; Sort and remove.
'...
'Show the final list of grades.
lstGrades.Items.Clear()
For Each grade In grades
lstGrades.Items.Add(grade)
Next

How to remove the two lowest numbers from a listbox in Visual Basic 2015?

Ok so I'm supposed to have the user input up to 13 grades, then have them averaged after removing the two lowest grades. I can't figure out what to use to remove the two lowest grades, whether it be from the listbox or during the calculation. Here's my code so far:
Dim strGrades As String
Dim decGrades As Decimal = 0
Dim decAverage As Decimal = 0
Dim decTotal As Decimal = 0
Dim strInputMessage As String = "Enter grade #"
Dim strInputHeading As String = "Grade"
Dim strNormalMessage As String = "Enter grade #"
Dim strNonNumericError As String = "Error - Please enter a grade"
Dim strNegativeError As String = "Error - Please enter a positive number"
Dim strCancelClicked As String = ""
Dim intMaxNumberOfEntries As Decimal = 13
Dim intNumberOfEntries As Decimal = 1
strGrades = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
Do Until intNumberOfEntries > intMaxNumberOfEntries Or strGrades = strCancelClicked
If IsNumeric(strGrades) Then
decGrades = Convert.ToDecimal(strGrades)
If decGrades >= 0 Then
lstGrades.Items.Add(decGrades)
decTotal += decGrades
intNumberOfEntries += 1
strInputMessage = strNormalMessage
Else
strInputMessage = strNegativeError
End If
Else
strInputMessage = strNonNumericError
End If
If intNumberOfEntries <= intMaxNumberOfEntries Then
strGrades = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
End If
Loop
lstGrades.Sorted = True
Assuming that you have an enumerable list of numeric values, e.g. an array of Decimal, then the most succinct way to get the average of all but the two lowest values would be:
Dim average = myList.OrderBy(Function(n) n).Skip(2).Avg()
I'm not sure how much of that would be acceptable for an assignment - if that's what this is - but the part that you're asking about specifically can still be done by calling Skip(2) if you start with a list that is sorted in ascending order.
You can count the entries in the list box and remove the bottom 2 entries.
You can add them to a list and sort it.

Getting wrong output in label

I have code that I have written, that has 3 labels for number of hurricanes, average hurricanes, and the year with the most hurricanes from a txt file. The code is working and the first 2 labels are displaying the correct results. However the last label is displaying the number of the year with the most hurricanes instead of the year.
Here is what I have:
Option Strict On
Public Class frmHurricaneStatistics
' Class level Private variables.
Public Shared _intSizeOfArray As Integer = 20
Private _strYears(_intSizeOfArray) As String
Private _intNumberOfHurricans(_intSizeOfArray) As Integer
Private Sub frmHurricaneStatistics_Load(sender As Object, e As EventArgs
) Handles MyBase.Load
' This load event reads the inventory text file and fills
' the ComboBox object with the Hurricane Statistics.
' Initialize an instace of the streamreader object and declare variables.
Dim objReader As IO.StreamReader
Dim strHurricaneStatistics As String = "Hurricanes.txt"
Dim intCount As Integer = 0
Dim intFill As Integer
Dim strFileError As String = "The file is not available. Please restart the
application when the file is available."
' Verify the Hurricane.txt file exists.
If IO.File.Exists(strHurricaneStatistics) Then
objReader = IO.File.OpenText(strHurricaneStatistics)
' Read the file line by line until the file is completed.
Do While objReader.Peek <> -1
_strYears(intCount) = objReader.ReadLine()
_intNumberOfHurricans(intCount) = Convert.ToInt32(objReader.ReadLine())
intCount += 1
Loop
objReader.Close()
' The ComboBox objext is filled with the Years for Hurricanes.
For intFill = 0 To (_strYears.Length - 1)
cmbYears.Items.Add(_strYears(intFill))
Next
Else
MsgBox(strFileError, , "Error")
Close()
' If ComboBox is filled then enable the Display Statistics button.
' btnDisplayStatistics.Enabled = True
End If
End Sub
Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
) Handles btnDisplayStatistics.Click
' This click event calls the sub procedures for the selected years and
' the number of hurricans in that year.
Dim intSelectedYear As Integer
Dim strMissingSelection As String = "Missing Selection"
Dim strSelectAYearError As String = "Please Select a Year"
' If the ComboBox object has a selection, Display Statistics.
If cmbYears.SelectedIndex >= 0 Then
intSelectedYear = cmbYears.SelectedIndex
Else
MsgBox(strSelectAYearError, , strMissingSelection)
End If
' The procedure MakeLabelsVisible Is called to display the labels
' And the results.
MakeLabelsVisible()
Dim intAverage As Double
Dim intYear As Integer
For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1
If intYear < _intNumberOfHurricans(intIndex) Then
intYear = _intNumberOfHurricans(intIndex)
End If
intAverage = intAverage + _intNumberOfHurricans(intIndex)
Next
intAverage = intAverage / _intNumberOfHurricans.Length
' Display the statistics for the Storm Average in the selected Year
' and the most active year within the range of year.
lblNumberOfHurricanes.Text = "The Number of Hurricanes in the Year " &
_strYears(intSelectedYear) & " is " & _intNumberOfHurricans(intSelectedYear).ToString() & "."
lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " & FormatNumber(intAverage, 0) & " Hurricanes."
lblMostStorms.Text = "The Year " & intYear & " Had The Most Storms Between " & (
_strYears(20) & " And " & (_strYears(0).ToString))
End Sub
Private Sub MakeLabelsVisible()
' This procedure displays the labels with the calculated results
lblNumberOfHurricanes.Visible = True
lblAvergeNumberHurricanes.Visible = True
lblMostStorms.Visible = True
End Sub
Updated code.
Looks like you're just populating intYear with the number of hurricanes?
intYear = _intNumberOfHurricans(intIndex)
I can't see where you're wanting to get a year value from. Does one even exist? Please post the rest of the code
Edit:
From what I understand (correct me if I'm wrong), you want the year that had the highest number of hurricanes? If so
Try
For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1
If _intNumberOfHurricans(intIndex) = _intNumberOfHurricans.Max Then
intYear = Integer.Parse(_strYears(intIndex))
End If
intAverage = intAverage + _intNumberOfHurricans(intIndex)
Next
What I'm doing here is looking for the highest value in _intNumberOfHurricans and comparing it to the number of hurricanes in the current iteration. If they're the same, then we are at the year with the highest number of hurricanes, so we populate intYear with _strYears(but as an Integer).
This code isn't perfect. For example, if the highest amount of hurricanes is 100, but there are 2 years where there are 100 hurricanes, it will only give the latest year, not the first year there were 100 hurricanes.
Because you set;
intYear = _intNumberOfHurricans(intIndex)
Not the year, number of hurricans. That should have point to a Year property.
intYear = _intNumberOfHurricans(intIndex).Year
Hope helps.

Visual Basic Keep validate InputBoxes in a Loop

I am working on a Budget calculator for a class project. Basically everything works great except one little annoying nuance.
When you don't enter a number or you enter a negative number you get an error message. However, when you accidentally type in a letter, get the error, then type in the negative number it just exits, and I want it to show that error and loop back until they enter in a positive number.
I am using VBA in Visual Studio 2012, this is a Windows Form Application.
Dim strEntertainmentHeading As String = "Entertainment Expenses"
Dim strHeading As String = "Budget Alottment"
Dim strNonNumericError As String = "Error - Enter a number for the Expense"
Dim strNegativeError As String = "Error - Enter a positive number for the Expense"
Dim strEntertainmentInput As String = "Enter the amount for Entertainment Expenses"
Dim strEntertainment As String
Dim decEntertainment As Decimal
strEntertainment = InputBox(strEntertainmentInput, strEntertainmentHeading, " ")
Do
If strEntertainment = "" Then
Exit Sub
ElseIf IsNumeric(strEntertainment) Then
decEntertainment = Convert.ToDecimal(strEntertainment)
If decEntertainment >= 0 Then
lstBudget.Items.Add("Entertainment Expense: " & decEntertainment.ToString("C2"))
' Display error message if user entered a negative value
Else
strEntertainmentInput = strNegativeError
End If
Else
strEntertainmentInput = strNonNumericError
End If
If decEntertainment <= 0 Then
strEntertainment = InputBox(strEntertainmentInput, strEntertainmentHeading, " ")
End If
Loop Until IsNumeric(strEntertainment) And decEntertainment >= 0
Of course, you have this problem: decEntertainment is 0 by default and after asking for input the second time, you do not Convert.ToDecimal anymore.
You avoid this type of errors if you adopt a clear coding style, like
Do
strEntertainmentInput = ''
strEntertainment = InputBox(strEntertainmentInput, strEntertainmentHeading, " ")
If Not IsNumeric(strEntertainment) Then
strEntertainmentInput = strNonNumericError
Else
decEntertainment = Convert.ToDecimal(strEntertainment)
If decEntertainment < 0 Then
strEntertainmentInput = strNegativeError
Else
lstBudget.Items.Add("Entertainment Expense: " & decEntertainment.ToString("C2"))
End
End
Loop Until strEntertainmentInput = ''

VB 2008 (Console) "For Each Loop" Lottery Results Comparison

I am facing an issue to perform numbers comparison between the integers inputted by user and random numbers generated by the codes.
Each of the integers input by the user should then be compared with the LOTTO numbers to check for a match. A For each… loop needs to be used to achieve this.
After checking all the 7 user input integers against the LOTTO numbers, the total number of matches should be output to the user. If there are no matches the output should read “LOOSER!”.
Here are my codes, I'm currently only stuck at the comparison portion, and we need to use for each loop to achieve this.
Imports System
Module Lotto
Sub Main()
'Declaration
Dim numbers(6) As Integer
Dim IsStarted As Boolean = True
'Prompt user to enter
Console.WriteLine("Please enter your 7 lucky numbers from 0 - 9 ONLY")
'Use Do While Loop to re-iterate the prompts
Do While IsStarted
For pos As Integer = 0 To 6
Console.Write("Enter number {0}: ", pos + 1)
'How it stores into an array
numbers(pos) = Console.ReadLine()
'Check if it is a number: use IsNumberic()
If IsNumeric(numbers(pos)) Then 'proceed
'Check if it is NOT 0 < x > 9
If numbers(pos) < 0 Or numbers(pos) > 9 Then
'Don't proceed
Console.WriteLine("Invalid Input")
IsStarted = True
'When any number is invalid, exit the loop
Exit For
End If
End If
IsStarted = False
Next
Loop
'Printing out the array. It can also be written as
'For pos = LBound(numbers) To UBound(numbers)
For pos = 0 To 6
Console.Write(numbers(pos) & " ")
Next
Console.WriteLine()
'Random number generator
Randomize()
Dim random_numbers(6) As Integer
Dim upperbound As Integer = 7
Dim lowerbound As Integer = 0
Dim rnd_number As Double = 0
For pos = 0 To 6
rnd_number = CInt((upperbound - lowerbound) * Rnd() + lowerbound)
random_numbers(pos) = rnd_number
Console.Write(random_numbers(pos) & " ")
Next
'Iterate and compare
Dim isSame As Boolean = False
Dim pos2 As Integer = 0
Dim Counter As Integer = 0
'For check = 0 To 6
'If numbers(pos2).Equals(random_numbers(pos2)) Then
For Each number As Integer In numbers
'Console.WriteLine(pos2 + 1 & ":" & number & ":")
If number.Equals(random_numbers(pos2)) Then
'Console.WriteLine("here is the number that matched:" & number & ":")
isSame = True
pos2 = pos2 + 1
End If
For Each num As Integer In random_numbers
If random_numbers Is numbers Then
Counter = Counter + 1
End If
Next
Next
Console.WriteLine()
'Display result
If isSame = True Then
Console.WriteLine("The total numbers of matches are: " & Counter)
Else
Console.WriteLine("LOOSER!")
End If
Console.ReadLine()
End Sub
End Module
Dim intCursor As Integer = 0
For Each intNumber As Integer In numbers
'Assumes first user chosen number must equal
'the first random number to be considered a match,
'the second user number must equal the second random,
'etc (Ordering is a factor).
If random_numbers(intCursor) = intNumber Then
Counter += 1
End If
intCursor += 1
Next
If (Counter > 0) Then
Console.WriteLine("The total numbers of matches are: " & Counter)
Else
Console.WriteLine("LOOSER!")
End If