So I'm doing this thing from the visual basics 2017 book where a table can hold 8 guests max and everything works fine except for when I have guest numbers that go up by factors of 4 (except factors of 8). 1 guest gives me 1 table but when I put 4 guest it gives me 2 and stays that way until I put 8 guests.
If dblGuest Mod dblTable = 0 Then
dblTotal = dblGuest / dblTable
ElseIf dblGuest Mod dbltable <> 0 Then
dblTotal = dblGuest / 8 1
End If
CodePudding user response:
Given that you're using Mod
, I would assume that you want integer division, rather than floating-point division. In that case, use \
, which is the integer division operator, rather than /
.
Personally, I'd be doing it something like this:
Module Module1
Sub Main()
Const guestCountPerTable As Integer = 8
For guestCount = 0 To 40
Console.WriteLine($"{guestCount} guest(s) requires {GetTableCount(guestCount, guestCountPerTable)} table(s)")
Next
End Sub
Private Function GetTableCount(guestCount As Integer, guestCountPerTable As Integer) As Integer
Dim tableCount = guestCount \ guestCountPerTable
If guestCount Mod guestCountPerTable <> 0 Then
tableCount = 1
End If
Return tableCount
End Function
End Module
You could also use the Math.DivRem
method:
Module Module1
Sub Main()
Const guestCountPerTable As Integer = 8
For guestCount = 0 To 40
Console.WriteLine($"{guestCount} guest(s) requires {GetTableCount(guestCount, guestCountPerTable)} table(s)")
Next
End Sub
Private Function GetTableCount(guestCount As Integer, guestCountPerTable As Integer) As Integer
Dim remainder As Integer
Dim tableCount = Math.DivRem(guestCount, guestCountPerTable, remainder)
If remainder <> 0 Then
tableCount = 1
End If
Return tableCount
End Function
End Module