1

こんにちは私はvb.netでプログラムを作成して、入力ボックスの値に1つの変数のみを使用して数の平均を見つけようとしていますが、カウント数の2番目は負である必要がありますが、正確な答えを得ることができませんここにコードがあります

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim num, count As Integer
    num = InputBox("Please enter number") 'for first entry
    While num > 0    ' here we have to check it that the num is not negative then to start
        num = InputBox("Please enter number")
        num += num
        count += 1             'this will calculate how many times number added    
    End While
    MsgBox("Average is " & num / count)


End Sub
4

2 に答える 2

2

このコードを使用してください...ループを終了する前に値を入れてはならないため、まだ一時変数が必要です

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim num, count As Integer
    count = 0
    num = 0
    While num >= 0    ' here we have to check it that the num is not negative then to start
        Dim temp As Integer
        temp = InputBox("Please enter number")
        If temp < 0 Then
            Exit While
        End If
        count += 1             'this will calculate how many times number added 
        num += temp
    End While
    MsgBox("Average is " & (num / count))
End Sub
于 2012-11-09T06:44:56.650 に答える
1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)        Handles Button1.Click
Dim num, count, avg As Integer
num = InputBox("Please enter number") 'for first entry
While num > 0    ' here we have to check it that the num is not negative then to start
    avg += num
    count += 1             'this will calculate how many times number added    
    num = InputBox("Please enter number")
End While
    MsgBox("Average is " & avg / count)
End Sub
于 2012-11-09T06:45:20.597 に答える