使用1个文本框和1个按钮查找用户输入的5个数字的总和 [英] Find the sum of 5 numbers entered by the user with 1 textbox and 1 button
问题描述
我需要找到5个总和。用户输入,只有1个文本框和1个按钮,每次输入一个否。在文本框中,我也单击按钮,然后清除文本框并重复该过程,直到我输入5个,第5个和最后一次单击按钮,msgbox将显示5个总和。由用户输入,并且将无法输入另一个号码。因为文本框不可见,因为限制只达到5次,这个问题的vb .net代码是什么?使用if-then条件语句和/或while循环。
我尝试过:
Dim s,i,n As Integer
s = 0
i = 0
n = TextBox1.Text
如果我< 5然后
i = i + 1
sum = sum + n
Msgbox(总和是:& sum)
结束如果
结束次级
结束班级
但它并没有真正起作用。不完整。
I need to find the sum of 5 nos. entered by the user, with only 1 textbox and 1 button, everytime I input a no. in the textbox, I also click the button, then clear the textbox and repeat the process until I have input 5 nos, the 5th and last time I clicked the button, the msgbox will show the sum of 5 nos. entered by the user, and will not be able to input another no. because the textbox will not be visible because the limit is reached only until 5 times, what is the vb .net code for this problem? Using if-then conditional statements and/or while loop only.
What I have tried:
Dim s, i, n As Integer
s = 0
i = 0
n = TextBox1.Text
If i < 5 Then
i = i + 1
sum = sum + n
Msgbox("The sum is : " & sum)
End If
End Sub
End Class
But its not really working. Incomplete.
推荐答案
以下是基于所列要求的工作示例。请注意,我给控件和变量的名称使代码非常容易阅读。
Below is a working example based on the requirements outlined. Notice that the names that I give controls and variables make the code very easy to read.
Public Class Form1
Private Total As Integer
Private Count As Integer
Private MaxEntries As Integer = 5
Private Sub ButAdd_Click(sender As Object, e As EventArgs) Handles ButAdd.Click
If String.IsNullOrEmpty(txtInput.Text.Trim) Then Return
Dim Value As Integer
If Integer.TryParse(txtInput.Text, Value) Then
If Count < MaxEntries Then
Count += 1
Total += Value
labTotal.Text = Total.ToString
Else
txtInput.Enabled = False
End If
End If
txtInput.ResetText()
txtInput.Select()
End Sub
End Class
这篇关于使用1个文本框和1个按钮查找用户输入的5个数字的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!