I'm writing a really simple weight calculator code -- user inputs weight and height, I calculate standard weight for that height, and the code compares that weight to ranges of weights based on the standard in an if/elseif block.
The standard weight is returned correctly, but the code ALWAYS returns "Normal Weight", regardless of the height to weight ratio. I'm new to VB, so my hunch is it's a relatively simple syntax issue.
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
lblFeedback.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight <= dbStWeight * 1.1) Then
lblResult.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 < dbWeight <= dbStWeight * 1.2) Then
lblResult.Text = ("Over Weight")
ElseIf (dbStWeight * 0.8 <= dbWeight < dbStWeight * 0.9) Then
lblResult.Text = ("Under Weight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
lblResult.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
lblResult.Text = ("Very underweight")
End If
lblFeedback.Refresh()
lblResult.Refresh()
Private Sub btOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
StWeight.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.1) Then
Result.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.2) Then
Result.Text = ("Overweight")
ElseIf (dbStWeight <= dbWeight) AndAlso (dbWeight < dbStWeight * 0.9) Then
Result.Text = ("Underweight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
Result.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
Result.Text = ("Very underweight")
End If
End Sub
Related
Private Sub BTNP1_Click(sender As Object, e As EventArgs) Handles BTNP1.Click
RollDice() 'dice rolling sub'
P1Pos = P1Pos + Dice + 1 'decides position based on dice roll'
Label1.Text = P1Pos
If P1Pos < 10 Then
P1x = 214 + (P1Pos * 60) 'Gets position and moves the PB (of size 60px) another 60px along from side'
P1Point = New Point(P1x, 570) 'Creates a new point with the new x value'
PBP1.Location = P1Point 'changes the location of the picture box'
ElseIf P1Pos > 9 And P1Pos < 20 Then
P1x = 754 - ((P1Pos - 10) * 60)
P1Point = New Point(P1x, 510)
PBP1.Location = P1Point
ElseIf P1Pos > 19 And P1Pos < 30 Then
P1x = 214 + ((P1Pos - 20) * 60)
P1Point = New Point(P1x, 450)
PBP1.Location = P1Point
ElseIf P1Pos > 29 And P1Pos < 40 Then
P1x = 754 - ((P1Pos - 30) * 60)
P1Point = New Point(P1x, 390)
PBP1.Location = P1Point
ElseIf P1Pos > 39 And P1Pos < 50 Then
P1x = 214 + ((P1Pos - 40) * 60)
P1Point = New Point(P1x, 330)
PBP1.Location = P1Point
ElseIf P1Pos > 49 And P1Pos < 60 Then
P1x = 754 - ((P1Pos - 50) * 60)
P1Point = New Point(P1x, 260)
PBP1.Location = P1Point
ElseIf P1Pos > 59 And P1Pos < 70 Then
P1x = 214 + ((P1Pos - 60) * 60)
P1Point = New Point(P1x, 200)
PBP1.Location = P1Point
ElseIf P1Pos > 69 And P1Pos < 80 Then
P1x = 754 - ((P1Pos - 70) * 60)
P1Point = New Point(P1x, 140)
PBP1.Location = P1Point
ElseIf P1Pos > 79 And P1Pos < 90 Then
P1x = 214 + ((P1Pos - 80) * 60)
P1Point = New Point(P1x, 80)
PBP1.Location = P1Point
ElseIf P1Pos > 89 And P1Pos < 94 Then
P1x = 754 - ((P1Pos - 90) * 60)
P1Point = New Point(P1x, 20)
PBP1.Location = P1Point
ElseIf P1Pos > 93 Then
P1Distance = 100 - P1Pos 'gets distance from end of board'
If P1Distance >= Dice + 1 Then 'aks if the distance is = or > the dice roll'
P1x = 754 - ((P1Pos - 90) * 60)
P1Point = New Point(P1x, 20)
PBP1.Location = P1Point
End If
End If
End Sub
my picture box character always stops at 94 or closer to 100 but never actually moves again once in this position. I'm trying to get the picture box to stay still unless it is able to move without exceeding 100. this is the only way I can think of doing it and it has worked in past iterations but I cannot get it to work here.
P.S. I can't use a different language because VB is required by my school for this project.
You could keep the checking for the game win separate from the movement of the player's piece.
Also, you could calculate the location of the player's piece, which would cut out many of the "magic numbers" used.
Option Strict On
Dim rand As New Random()
Dim P1Pos As Integer = -1
Private Function DieRoll() As Integer
' Return a random number from 1 to 6 inclusive.
Return rand.Next(1, 7)
End Function
Private Sub BTNP1_Click(sender As Object, e As EventArgs) Handles BTNP1.Click
Dim currentSquare = P1Pos
Dim numberRolled = DieRoll() ' 1..6
Dim provisionalSquare = currentSquare + numberRolled
If provisionalSquare <= 100 Then
currentSquare = provisionalSquare
Else
' Perhaps indicate that the number rolled was too high, so no move.
MessageBox.Show("Rolled too high!")
End If
If currentSquare = 100 Then
' I guess you want to do something here
Exit Sub
End If
P1Pos = currentSquare
Dim row = currentSquare \ 10
Dim col = currentSquare Mod 10
Dim direction = 1 - ((row Mod 2) * 2) ' ltr=1 rtl=-1
If direction = -1 Then
col = 9 - col
End If
Dim x = 214 + 60 * col
Dim y = 510 - 60 * row
PBP1.Location = New Point(x, y)
End Sub
The calculations for x and y are just what I guessed at from the code in the question.
If this is for homework, you need to make sure that you understand what is happening in the code and be able to explain it. Some study of what is going on using the debugger to watch the values of variables will go a long way to helping you with that and your future endeavours ;)
Here is my code:
Dim Weight, Height, Bmi_value As Integer
Weight = TextBox1.Text
Height = TextBox2.Text
Bmi_value = (Weight / Height ^ 2)
TextBox3.Text = Bmi_value
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub
This is your code fixed:
'You were using Integer instead of Double
Dim Weight, Height, Bmi_value As Double
If you want to take the value from the textBoxes, you have to covert it to Double.
Weight = Convert.ToDouble(TextBox1.Text)
Height = Convert.ToDouble(TextBox2.Text)
'Better to call the right function Math.Pow()
Bmi_value = (Weight / Math.Pow(Height, 2))
'You have to convert it to String
TextBox3.Text = Convert.ToString(Bmi_value)
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub
There was alot of error in this code:
First: don't declare var as integer if you need decimal value, like with the BMI calculation.
Second: always convert your value to the right type if you are getting them from TextBox or if you want to print them inside a TextBox
Dim dblWeight As Double, dblHeight As Double, dblBMI As Double
dblWeight = CDbl(TextBox1.Text) 'assumes lbs.
dblHeight = CDbl(TextBox2.Text) 'assumes inches
On Error GoTo 0
dblBMI = ((dblWeight * 703.0#) / (dblHeight * dblHeight))
TextBox3.Text = dblBMI
Select Case dblBMI
Case Is <= 18.5 : TextBox4.Text = "Underweight"
Case 18.6 To 24.9 : TextBox4.Text = "Normal"
Case 25.0# To 29.9 : TextBox4.Text = "Overweight"
Case 30.0# To 34.9 : TextBox4.Text = "Obese Class I"
Case 35.0# To 39.9 : TextBox4.Text = "Obese Class II"
Case Is >= 40.0# : TextBox4.Text = "Obese Class III"
End Select
Exit Sub
Try this, Works for me.
I am making a gravity/solar system simulation and when the simulation runs I am only getting about 5 fps. Here is the relevant part of my code:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles picSpace.Paint
earth.displayX = Math.Round(earth.positionX)
earth.displayY = Math.Round(earth.positionY)
e.Graphics.FillEllipse(Brushes.Blue, earth.displayX - 5, earth.displayY - 5, 10, 10)
e.Graphics.FillEllipse(Brushes.Yellow, sunX - 10, sunY - 10, 20, 20)
distance()
position()
End Sub
Sub distance()
dX = sunX - earth.positionX
dY = sunY - earth.positionY
If (earth.positionX >= sunX) And (earth.positionY <= sunY) Then
dX *= -1
Else
If (earth.positionX >= sunX) And (earth.positionY >= sunY) Then
dX *= -1
dY *= -1
Else
If (earth.positionX <= sunX) And (earth.positionY >= sunY) Then
dY *= -1
Else
If (earth.positionX <= sunX) And (earth.positionY <= sunY) Then
'do nothing
End If
End If
End If
End If
d = Math.Sqrt((((dY) * 1000000) ^ 2) + (((dX) * 1000000) ^ 2))
d = d * 1000
End Sub
Sub position()
If first = False Then
earth.positionX += ((((earth.oldVelocityX + earth.velocityX) / 2) * simulationSpeed) / 1000000000)
earth.positionY += ((((earth.oldVelocityY + earth.velocityY) / 2) * simulationSpeed) / 1000000000)
orbit(0, counter) = earth.positionX
orbit(1, counter) = earth.positionY
counter += 1
ReDim Preserve orbit(1, counter)
lblPositionX.Text = "X: " & Math.Truncate(earth.positionX)
lblPositionY.Text = "Y: " & Math.Truncate(earth.positionY)
End If
F = (earth.mass * sunMass * G) / (d ^ 2)
theta = Math.Atan(dX / dY)
If (earth.positionX >= sunX) And (earth.positionY <= sunY) Then
earth.forceX = F * Math.Sin(theta) * -1
earth.forceY = F * Math.Cos(theta)
Else
If (earth.positionX >= sunX) And (earth.positionY >= sunY) Then
earth.forceX = F * Math.Sin(theta) * -1
earth.forceY = F * Math.Cos(theta) * -1
Else
If (earth.positionX <= sunX) And (earth.positionY >= sunY) Then
earth.forceX = F * Math.Sin(theta)
earth.forceY = F * Math.Cos(theta) * -1
Else
If (earth.positionX <= sunX) And (earth.positionY <= sunY) Then
earth.forceX = F * Math.Sin(theta)
earth.forceY = F * Math.Cos(theta)
End If
End If
End If
End If
a = F / earth.mass
earth.accelerationX = earth.forceX / earth.mass
earth.accelerationY = earth.forceY / earth.mass
earth.oldVelocityX = earth.velocityX
earth.oldVelocityY = earth.velocityY
earth.velocityX = earth.oldVelocityX + (earth.accelerationX * simulationSpeed)
earth.velocityY = earth.oldVelocityY + (earth.accelerationY * simulationSpeed)
first = False
Me.Refresh()
End Sub
Originally I had a large portion of the code in a do...loop and the framerate was fine, but I could not interact with any controls while the loop was running. Doing it as shown above lets me interact with controls but the framerate is very choppy. Any help would be greatly appreciated.
Calling Refresh from within Paint is very odd. One possible performance issue is that this forces the entire form to repaint including the background. I would suggest 2 things:
Create a timer object, and perform the calculation and updates from within Timer_Tick event.
Then remove the Me.Refresh command from sub position(), so that position() and distance() just do calculations. Add a call to Me.Invalidate() at the beginning and end of the Timer_Tick passing it the rectangle containing the location of the earth. This will force only the old and new locations to be repainted and won't repaint a lot of unchanged background. Your Paint method is then likely to just be the 2 FillEllipse lines.
I am trying to do some computations on VB.net. I used if else statement since I'm a bit familiar with it. My code goes like this
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 < a < 9999.99) Then
Label5.Text = a - 225
ElseIf (10000 < a < 10999.99) Then
Label5.Text = a - 250
ElseIf (11000 <= a < 11999.99) Then
Label5.Text = a - 275
ElseIf (12000 <= a < 12999.99) Then
Label5.Text = a - 300
ElseIf (13000 <= a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
ElseIf a = "" Then
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The farthest the condition was able to go to -225 even if I put 20000 in it. It will only subtract 225 from the 20000. Is there something wrong on what I did or is there a better way to do it?
Looks like you're missing some AndAlso from your If/ElseIf statements. Normally, you wouldn't be able to go from String to Integer without doing a conversion, so I'm guessing you don't have Option Strict
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 <= a AndAlso a < 10000) Then
Label5.Text = a - 225
ElseIf (10000 <= a AndAlso a < 11000) Then
Label5.Text = a - 250
ElseIf (11000 <= a AndAlso a < 12000) Then
Label5.Text = a - 275
ElseIf (12000 <= a AndAlso a 13000) Then
Label5.Text = a - 300
ElseIf (13000 <= a AndAlso a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a AndAlso a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a AndAlso a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a AndAlso a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a AndAlso a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a AndAlso a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a AndAlso a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a AndAlso a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a AndAlso a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a AndAlso a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a AndAlso a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a AndAlso a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a AndAlso a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a AndAlso a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a AndAlso a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a AndAlso a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a AndAlso a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a AndAlso a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a AndAlso a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a AndAlso a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a AndAlso a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I wouldn't use such a giant conditional branch, I'd find a more algorithmic approach.
Example:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim salary As Integer = 34567, subtractor As Integer = 200
Dim r As Range = GetSalaryRange(salary)
For I As Integer = 9000 To r.Lower Step 1000
subtractor += 25
Next
Label5.Text = (salary - subtractor).ToString
End Sub
Function GetSalaryRange(salary As Integer) As Range
If salary < 9000 Then Return New Range() With {.Lower = 0, .Upper = 8999}
Dim remainder As Integer = salary Mod 1000
Return New Range With {.Lower = salary - remainder, .Upper = salary - remainder + 999}
End Function
Public Class Range
Public Lower, Upper As Integer
Public Function Contains(number As Integer) As Boolean
If number >= Lower AndAlso number <= Upper Then Return True Else Return False
End Function
End Class
End Class
For some reason when I run this it only ever calculates to 0
Where am I going wrong? :(
The user has 3 input boxes to place values. From there those values should be calculating. It only ever equals a value of 0. I get no errors
Option Strict On
Public Class Form1
Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
Dim dblPacA, dblPacB, dblPacC, dblAnswerA, dblAnswerB, dblAnswerC, dblGrandTotal As Double
Dim dblAnswerA1, dblAnswerB1, dblAnswerC1 As Double
'Packages Retail
Dim dblPACA_FACTOR As Double = 99
Dim dblPACB_FACTOR As Double = 199
Dim dblPACC_FACTOR As Double = 299
'Rate of each range
Dim dblTENNINE_FACTOR As Double = 0.8
Dim dblTWONINE_FACTOR As Double = 0.7
Dim dblFIVENINE_FACTOR As Double = 0.6
Dim dblONETEN_FACTOR As Double = 0.5
Try
'important calculate
dblAnswerA1 = dblPacA * dblPACA_FACTOR
dblAnswerB1 = dblPacB * dblPACB_FACTOR
dblAnswerC1 = dblPacC * dblPACC_FACTOR
dblPacA = CDbl(txtPacA.Text)
dblPacB = CDbl(txtPacB.Text)
dblPacC = CDbl(txtPacC.Text)
dblGrandTotal = dblAnswerA + dblAnswerB + dblAnswerC
lblGrandTotal.Text = "Gran Total:" & (dblGrandTotal.ToString("c"))
'lblAnswer.Text = dblAnswer.ToString
'lblAnswer.Text = "PackageA:" & dblAnswerA _
' & "PackageB:" & dblAnswerB & "PackageC:" _
'& dblAnswerC & "GrandTotal:" & dblGrandTotal
Catch
End Try
If dblPacA >= 0 Then
If dblPacA < 10 Then
dblAnswerA = dblAnswerA1
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 10 And dblPacA < 20 Then
dblAnswerA = dblAnswerA1 * dblTENNINE_FACTOR
lblAnswerA.Text = "PackageA:" & (dblAnswerA.ToString("c"))
ElseIf dblPacA >= 20 And dblPacA < 50 Then
dblAnswerA = dblAnswerA1 * dblTWONINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblPacA >= 50 And dblPacA < 100 Then
dblAnswerA = dblAnswerA1 * dblFIVENINE_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
ElseIf dblAnswerA >= 100 Then
dblAnswerA = dblAnswerA1 * dblONETEN_FACTOR
lblAnswerA.Text = "PackageA:" & dblAnswerA.ToString("c")
End If
Else
MessageBox.Show("txtPacA must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacB >= 0 Then
If dblPacB >= 10 And dblPacB <= 19 Then
dblAnswerB = dblAnswerB1 * dblTENNINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 20 And dblPacB <= 49 Then
dblAnswerB = dblAnswerB1 * dblTWONINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
ElseIf dblPacB >= 50 And dblPacB <= 99 Then
dblAnswerB = dblAnswerB1 * dblFIVENINE_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
Else
dblAnswerB = dblAnswerB * dblONETEN_FACTOR
lblAnswerB.Text = "PackageB:" & dblAnswerB.ToString("c")
End If
Else
MessageBox.Show("txtPacB must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
If dblPacC >= 0 Then
If dblPacC >= 10 And dblPacC <= 19 Then
dblAnswerC = dblAnswerC1 * dblTENNINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 20 And dblPacA <= 49 Then
dblAnswerC = dblAnswerC1 * dblTWONINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
ElseIf dblPacC >= 50 And dblPacC <= 99 Then
dblAnswerC = dblAnswerC1 * dblFIVENINE_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
Else
dblAnswerC = dblAnswerC1 * dblONETEN_FACTOR
lblAnswerC.Text = "PackageC:" & dblAnswerC.ToString("c")
End If
Else
MessageBox.Show("txtPacC must be greater than or equal to 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
End Class
dblAnswerA1 = dblPacA * dblPACA_FACTOR
where is dblPacA 's value set? its not. its 0