vb.net - Visual basic编写最小数程序
问题描述
我是 Visual Basic 的新手,我正在尝试编写一个程序来确定三个中最小的数字。每次我运行程序时,它都不会弹出消息框。这是我到目前为止所拥有的:
公开课形式1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TextBox1 As Integer
Dim TextBox2 As Integer
Dim TextBox3 As Integer
TextBox1 = Val(TextBox1)
TextBox2 = Val(TextBox2)
TextBox3 = Val(TextBox3)
If TextBox1 < TextBox2 And TextBox1 < TextBox3 Then
MessageBox.Show(TextBox1)
End If
End Sub
解决方案
您还可以将 .Min 与数组一起使用来查找最小值。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Declare an array of type Integer with 3 elements
Dim value(2) As Integer
'A variable to hold the parsed value of TextBoxes
Dim intVal As Integer
If Integer.TryParse(TextBox1.Text, intVal) Then
'Assign the parsed value to an element of the array
value(0) = intVal
End If
If Integer.TryParse(TextBox2.Text, intVal) Then
value(1) = intVal
End If
If Integer.TryParse(TextBox3.Text, intVal) Then
value(2) = intVal
End If
'Use .Min to get the smalles value
Dim minNumber As Integer = value.Min
MessageBox.Show($"The smalles value is {minNumber}")
'Or
'in older versions of vb.net
'MessageBox.Show(String.Format("The smallest value is {0}", minNumber))
End Sub
推荐阅读
- python-3.x - Selenium 在不同屏幕中打开浏览器
- javascript - 使用 Jquery 更新 HTML 附加项
- php - mysqli (oop) 在 mysql 服务器上运行但不是 mariadb
- mongodb - 带有数组元素的 MongoDB 数学运算
- java - 更改我的输出 java/Sparql 的类型
- php - 如何使用 Laravel 在 Google 地图上添加更多的两端方向
- python - Python UUID 字节序混淆
- php - 检查日期是否具有特定格式
- javascript - 当该点可能会沿途改变时,CSS从特定点缩放?
- python - 设置新的用户数据目录后硒无法打开页面