Do While Loop - 如何删除列表中的最后一个数字?

时间:2010-09-29 19:33:27

标签: vb.net

在VB.net中学习循环(仍然是初学者)。我有下面的代码,基本上它是为了一旦它达到20以上的数字就停止循环。现在,问题是它确实在20号后停止,但列表中显示的最后一个数字总是高于20。 ....任何想法我怎么能阻止它显示最后一个数字20以上?

如果你们知道它可以解释一下这个答案 - 想学习以及有代码......谢谢!

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.Add(intRandomNumber.ToString)
    Loop
End Sub

6 个答案:

答案 0 :(得分:3)

问题是你在生成之后立即添加随机项,而不检查是否应该添加它。

您可以在添加之前检查该号码是否应添加到列表中:

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

或者,打破循环:

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

答案 1 :(得分:2)

问题是您在检查号码之前选择一个新号码并将其添加到列表中。您应该选择该号码,进行检查,然后将其添加到列表中。

在循环中有几种方法可以做到这一点。

在循环之前初始化,并在循环结束时创建一个新数字:

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

检查循环中间并退出:

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

仔细检查:

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

答案 2 :(得分:1)

......好吧......如你所说,当'intRandomNumber'大于或等于20时,你的循环停止

......所以...最后,数字将大于或等于二十;)

我不知道确切的语法,但您可以执行以下操作:

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

答案 3 :(得分:0)

就Do While ..循环结构而言,在检查条件之前执行循环体。

如果要在执行循环体之前检查,请查看While ... End构造。

答案 4 :(得分:0)

添加到Oded的答案,这样您就不会进行两次检查

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

答案 5 :(得分:0)

注意随机声明的放置

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