Do..While ...循环结果

时间:2011-03-22 18:53:07

标签: vb.net loops do-while

以下伪代码的结果应该是什么:

将计数器初始化为10

同时反击< 100

显示计数器乘以2

向计数器添加10

结束循环

我在想:20,60,140

这是我的代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim multiplied As Integer
    Dim counter As Integer = 10
    Do While counter < 100
        multiplied = counter * 2
        Label1.Text = Label1.Text & ControlChars.NewLine & multiplied.ToString
        counter = multiplied + 10
    Loop
End Sub

谢谢你们!

1 个答案:

答案 0 :(得分:4)

  

显示计数器乘以2

该指令中没有任何内容可以修改计数器。基于对指令的严格解释,输出应如下所示:

  

20 40 60 80 100 120 140 160 180

但是,您的代码与您期望的结果相符。如果您想要符合您指示的代码,请执行以下操作:

Dim counter As Integer = 0
Dim result As New StringBuilder()
Dim delimiter As String = ""
Do While counter < 100
   result.Append(delimiter).Append( (counter*2).ToString() )
   delimiter = Environment.NewLine
   counter += 10
Loop
Label1.Text = result.ToString()

为了好玩,我们可以做这样的事情:

Label1.Text = Enumerable.Range(1, 9)
        .Select(Function(i) i * 10)
        .Aggregate("", Function(s, i) s = s & i.ToString() & ",")