开发者

Do While Loop - how to remove the last number in a list?

learning about loops (still a beginner) in VB.net. I have got the below code and basically it is meant to stop the loop once it reaches a number above 20. Now, the problem is that it does stop after number 20 but the last number that is displayed in the list is always above 20.....any idea how I can stop it showing the last number as above 20?

If you guys know it can you also explain a bit about the answer - would like to learn as well as have the code..Thanks!

Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
    Dim objRandom As New Random
    Dim intRandomNumber As Integer = 0
    ClearList()

    Do While intRandomNumber < 20
        intRandomNumber = objRandom.Next(25)
        lstData.Items开发者_JAVA百科.Add(intRandomNumber.ToString)
    Loop
End Sub


The problem is that you are adding the random item immediately after it was generated, without checking whether you should add it.

You can check that the number should be added to the list before adding it:

Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
    Dim objRandom As New Random
    Dim intRandomNumber As Integer = 0
    ClearList()

    Do While intRandomNumber < 20
        intRandomNumber = objRandom.Next(25)
        If intRandomNumber < 20 Then
            lstData.Items.Add(intRandomNumber.ToString)
        End If
    Loop
End Sub

Or, break out of the loop:

Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
    Dim objRandom As New Random
    Dim intRandomNumber As Integer = 0
    ClearList()

    Do While intRandomNumber < 20
        intRandomNumber = objRandom.Next(25)
        If intRandomNumber >= 20 Then
            Exit While
        End If

        lstData.Items.Add(intRandomNumber.ToString)
    Loop
End Sub


The problem is that you are picking a new number and adding it to the list before checking the number. You should pick the number, check it, and then add it to the list.

There are several ways of doing that in a loop.

Initialise before the loop, and create a new number at the end of the loop:

Dim objRandom As New Random
Dim intRandomNumber As Integer
ClearList()

intRandomNumber = objRandom.Next(25)
Do While intRandomNumber < 20
  lstData.Items.Add(intRandomNumber.ToString)
  intRandomNumber = objRandom.Next(25)
Loop

Check in the middle of the loop and exit:

Dim objRandom As New Random
Dim intRandomNumber As Integer
ClearList()

Do
  intRandomNumber = objRandom.Next(25)
  if intRandomNumber >= 20 Then Exit Loop
  lstData.Items.Add(intRandomNumber.ToString)
Loop

Double check:

Dim objRandom As New Random
Dim intRandomNumber As Integer
ClearList()

Do
  intRandomNumber = objRandom.Next(25)
  If intRandomNumber < 20 Then
    lstData.Items.Add(intRandomNumber.ToString)
  End If
Loop Until intRandomNumber >= 20


...well ...as you said, your loop stops when 'intRandomNumber' is greater or equal to twenty

...so ...at the end, the number will be greater or equal to twenty ;)

I don't know the exact syntax, but you can do something like:

Do While True
    intRandomNumber = objRandom.Next(25)
    if intRandomNumber >= 20
        Break
    lstData.Items.Add(intRandomNumber.ToString)
Loop


As far as Do While .. Loop constructs go, the body of the loop is executed BEFORE condition is checked.

If you want to check BEFORE executing the body of the loop, look into a While ... End construct.


To Add on to Oded's answer so you don't do your check twice

Private Sub btnDoWhileLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoWhileLoop.Click
    Dim objRandom As New Random
    Dim intRandomNumber As Integer = 0
    ClearList()

    Do
        intRandomNumber = objRandom.Next(25)
        If intRandomNumber < 20 Then
            lstData.Items.Add(intRandomNumber.ToString)
        Else
            Exit Loop
        End If
    Loop
End Sub


Note placement of Random statement

Public Class Form1

    Dim objRandom As New Random 'usually best declared at the class level

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                          Handles Button1.Click

        Dim intRandomNumber As Integer = 0

        lstData.Items.Clear()

        Do

            intRandomNumber = objRandom.Next(25)

            If intRandomNumber < 20 Then
                lstData.Items.Add(intRandomNumber.ToString)
            End If

        Loop While intRandomNumber < 20

    End Sub

End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜