vb.net - 带循环的数字总和
问题描述
因此,对于我的班级,我正在尝试编写一个总结 1-10 数字的代码。例如,如果用户输入 3,则该程序将添加 1+2+3,最终答案将是 6.......我正在尝试使用循环以及显示消息来执行此操作回答。
这是我到目前为止的代码........
Option Strict On
Public Class frmSumOfNumbers
Private Sub btnEnterNumbers_Click(sender As Object, e As EventArgs) Handles btnEnterNumbers.Click
'For loop from 0 to counter -1
InputBox("Enter A Positive integer value", "Input needed", "10")
End Sub
Function Validation(ByVal PositiveNumber As Double, ByRef Result As Double) As Boolean
If PositiveNumber > -1 Then
Result = CDbl(PositiveNumber)
Else
'pop message box and return false if not positive
MessageBox.Show("Please enter positive numbers only")
Return False
End If
End Function
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Closes the program
Close()
End Sub
End Class
解决方案
您的第一个问题是提供一个地方来收集用户的输入值。看看 InputBox 函数https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.interaction.inputbox?view=netframework-4.8
请注意,它返回一个字符串。我们可以通过提供一个字符串变量来获取用户的输入。
Dim UserInput As String
UserInput = InputBox("Enter A Positive integer value", "Input needed", "10")
但是一旦我们到达 End Sub,这个值就会消失!如果我们使用表单级别的变量来收集值,只要表单打开,该变量就会存在。表单级别(类级别)变量的另一个优点是它可以通过 from 中的任何方法出现。
我们可以使用集合变量,如数组或列表。由于我们不确定用户将输入多少个数字,所以我们使用一个列表。对于数组,每次我们得到一个新数字来调整数组大小时,我们都必须使用 ReDim Preserve。列表没有必要这样做。请参阅https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netcore-3.1
在表单级别...
Private IntegerList as New List(Of Integer)
但是我们有一个来自用户的字符串变量。这就是您的验证代码的用武之地。我们需要传递用户在输入框中输入的内容(记住 InputBox 返回一个字符串),因此字符串中的参数的数据类型。我们想要返回一个整数,所以函数的数据类型是整数。函数中的任何 return 语句必须后跟一个整数值。
我使用 Integer.TryParse 测试输入是否为数字,然后我测试该数字是否为正。请参阅文档https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netcore-3.1
如果用户输入通过验证,则将数字添加到列表中,否则显示消息。
要添加列表,您可以使用 .Net 框架在幕后使用列表的 .Sum 方法进行循环,或者您可以使用 For Each 循环自己进行。结果相同。
Private IntegerList As New List(Of Integer)
Private Sub AddANumber_Click(sender As Object, e As EventArgs) Handles AddANumber.Click
Dim UserInput = InputBox("Enter A Positive integer value", "Input needed", "10")
Dim RetVal = Validation(UserInput)
If RetVal > -1 Then
IntegerList.Add(RetVal)
Else
MessageBox.Show("Please enter positive numbers only")
End If
End Sub
Function Validation(ByVal UserInput As String) As Integer
Dim ReturnInteger As Integer
If Integer.TryParse(UserInput, ReturnInteger) AndAlso ReturnInteger > -1 Then
Return ReturnInteger
Else
Return -1
End If
End Function
Private Sub DisplayTotal_Click(sender As Object, e As EventArgs) Handles DisplayTotal.Click
Dim Total = IntegerList.Sum
MessageBox.Show($"The Total is {Total}")
Dim LoopTotal As Integer
For Each i In IntegerList
LoopTotal += i
Next
MessageBox.Show($"The LoopTotal is {LoopTotal}")
End Sub
注意:以 $ 开头的字符串称为插值字符串。您可以搜索该术语以了解它们的工作原理。
推荐阅读
- laravel - 社交名流谷歌登录 laravel 5.8 中的客户端错误 500
- java - 保存使用 onTouchEvent 绘制到位图上的图像
- c# - 如何为 .Net Core Web 应用程序进行单元测试依赖关系解析?
- jquery - 如何在页面中嵌入 div,或如何剥离所有其他 div
- javascript - 推断参数的类型以避免泛型?
- python - 编写斐波那契函数
- java - 为什么我无法添加到链表的末尾?
- python - unicode vs 字符:什么是'\x10'
- android - 如何在 Android Studio 的 ConstraintLayout 中设置垂直控件的高度?
- r - 如何将直方图和密度图与 Y 轴上的数字而不是密度重叠