簡體   English   中英

Visual Basic使用輸入框執行while循環

[英]Visual Basic do while loop with inputbox

我正在創建一個程序,用戶在其中輸入正數。 用戶可以輸入任意數量的內容。 但是,如果用戶輸入一個負值或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

基本的問題是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM