Visual Basic使用输入框执行while循环

时间:2018-07-19 05:37:54

标签: vb.net

我正在创建一个程序,用户可以在其中输入正数。用户可以输入任意数量的内容。但是,如果用户输入一个负值或0值,则程序结束。

程序还将根据用户输入的时间来计算平均值。它还必须打印用户提供的最小和最大数量。例如,如果用户输入以下数字字符串:5、4、9、11、15、2,则最小数字为2,最大数字为15。

无论如何,我遇到的问题是该程序仅显示平均值和最大值。我不知道如何显示最小值。任何帮助将不胜感激!

这是我的代码:

Public Class Form1
    Private Sub btnGetNumbers_Click(sender As Object, e As EventArgs) Handles btnGetNumbers.Click
        Dim min As Integer ,max As Integerm, count As Integer 'Declare variables
        Dim total As Integer,average As Double, num As Integer 
        max = num 'Store the max vaule
        min = num 'Store the min vaule
        count = 0 'Set the count value
        total = 0  'Set the total value
        Do 'Start the loop
            Dim response = InputBox("Enter numbers", "Enter numbers") 'Ask the user for input numbers
            total = total + num 'Find the value
            count = count + 1  'Increment the count
            If num > max Then max = num 'set max value if number is max
            If num < min Then min = num 'set min value if number is min
            num = response 'Read the number of the user input
        Loop While num > 0  'The loop ends here
        average = CDbl(total) / CDbl(count) 'Find the average value
        txtMinimum.Text = min 'Display the min value
        txtMaximum.Text = max 'Display the max value
        txtAverage.Text = average 'Display the average value
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

基本问题是num(因此min)被初始化为0。由于您的程序(通过设计)在输入值<= 0时结束,因此min为永远不会更新,因为每个用户条目都>0。此外,如果使用者输入0来结束进程...它将变为最小值。试试这个示例的编辑版本:

Private Sub CommandButton1_Click()
        'Declare minimum variable
        Dim min As Integer
        'Declare maximum variable
        Dim max As Integer
        'Declare count variable
        Dim count As Integer
        'Declare total variable
        Dim total As Integer
        'Declare average variable
        Dim average As Double
        'Declare number variable
        Dim num As Integer



        'Store the max value
        max = num

        'Store the min value
        min = 32767 'initialize to the maximum possible Int value rather than 0

        'Set the count value
        count = 0

        'Set the total value
        total = 0

        'Start the loop
        Do

            'Ask the user for input numbers
            'Dim response = InputBox("Enter numbers", "Enter numbers")

            Dim response As String
            response = InputBox("Enter numbers", "Enter numbers")

            num = CInt(response)

            If num > 0 Then 'avoid overwriting the min

                'Find the value
                total = total + num

                'Increment the count
                count = count + 1

                'Check if the number is max
                If num > max Then
                    'Set the max value
                    max = num
                End If

                'Check if the number is min
                If num < min Then
                    'Set the value of min
                    min = num
                End If

            End If

            'The loop ends here
        Loop While num >= 1

        If count > 0 Then
            'Find the average value
            average = CDbl(total) / CDbl(count)

            'Display the min value
            'txtMinimum.Text = min
            MsgBox "Min = " & CStr(min)

            'Display the max value
            'txtMaximum.Text = max
            MsgBox "Max = " & CStr(max)

            'Display the average value
            'txtAverage.Text = average
            MsgBox "Avg = " & CStr(average)
        End If

    End Sub