开发者

Do..While...Loop Result

开发者 https://www.devze.com 2023-02-19 14:15 出处:网络
What should the results be of the following pseudocode: Initialize counter to 10 Do while counter < 100

What should the results be of the following pseudocode:

Initialize counter to 10

Do while counter < 100

Display counter multiplied by 2

Add 10 to the counter

End loop

I'm thinking: 20, 60, 140

This is my code:

Private Sub 开发者_C百科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

Thanks guys!!


Display counter multiplied by 2

Nothing in that instruction says to modify the counter. Based on a strict interpretation of your instructions, the output should look like this:

20 40 60 80 100 120 140 160 180

Your code, however, matches the results you expect. If you want code that matches your instructions, do it like this:

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()

And for fun we could do something like this:

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

精彩评论

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