嵌套循环并将结果输入到列表框中

时间:2015-03-19 20:52:32

标签: vb.net loops listbox inputbox

我是VB的新手,我需要制作一个程序,在输入框中输入每100个符号的@符号条形图。这重复4次并被舍入,这样440将给出4个符号,450将给出5个符号。这是我检索每个游戏出勤率的代码,我只需要帮助形成一个循环,将结果显示在名为lstChart的列表框中。

    Private Sub btnChart_Click(sender As Object, e As EventArgs) Handles btnChart.Click
    'Retrieve attendance for game 1
    Dim strAttendance1 As Integer
    strAttendance1 = InputBox("Enter Attendance for Game #1.", "Chart Input")
    If strAttendance1 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 2
    Dim strAttendance2 As Integer
    strAttendance2 = InputBox("Enter Attendance for Game #2.", "Chart Input")
    If strAttendance2 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 3
    Dim strAttendance3 As Integer
    strAttendance3 = InputBox("Enter Attendance for Game #3.", "Chart Input")
    If strAttendance3 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

    'Retrieve attendance for game 4
    Dim strAttendance4 As Integer
    strAttendance4 = InputBox("Enter Attendance for Game #4.", "Chart Input")
    If strAttendance4 < 0 Then
        MessageBox.Show("Attendance must be greater than zero.", "Error")
    End If

1 个答案:

答案 0 :(得分:1)

您可以使用String的构造函数重复字符x次。唯一棘手的部分是.NET的默认舍入为banker's rounding,所以如果你总是想要.5来整理,你需要具体:

' This could be simplified by using an array (or List) for the values.
' Also, interesting use of Hungarian notation, since these are Integers!
For Each attendance In {strAttendance1, strAttendance2, strAttendance3, strAttendance4}
    Dim x = Math.Round(attendance / 100, MidpointRounding.AwayFromZero)
    lstChart.Items.Add(New String("@"c, x))
Next