vb.Net检查两个最大的数字是什么

时间:2016-08-17 12:42:00

标签: vb.net

我有4个文本框接受来自用户的数字,我想检查输入的两个最大数字是什么,可以使用if then else语句来完成

Dim Big_num_1 As Integer
Dim Big_num_2 As Integer

'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click  
    'if then else statements
End Sub

我是Vb.net的初学者,我真的很感激我能得到的任何帮助 如果可能的话,我想要一个If then else声明解决方案

3 个答案:

答案 0 :(得分:0)

Private Sub Docheck()
    Dim Numbers As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
    Dim Ordered As String() = Numbers.OrderByDescending(Function(x) CInt(x)).ToArray
    Dim Highest As Integer = CInt(Ordered(0))
    Dim SecondHighest As Integer = CInt(Ordered(1))
    MessageBox.Show(String.Concat(Highest , "  " , SecondHighest))
End Sub

在您点击的按钮点击事件中

Docheck()

答案 1 :(得分:0)

可以使用If / Else语句,但这样做很难:

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
    Dim Numbers = { TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text }
    Dim results = Numbers.Select(Function(s) Integer.Parse(s)).OrderByDescending().Take(2).ToList()

    Big_num_1 = results(0)
    Big_num_2 = results(1)
End Sub

答案 2 :(得分:0)

这可以通过像这样的if语句来完成..

Dim Big_num_1 As Integer = 0
Dim Big_num_2 As Integer = 0

'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer

Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click  
    Dim numList(4) as Integer
    numList(0) = txtbox_1
    numList(1) = txtbox_2
    numList(2) = txtbox_3
    numList(3) = txtbox_4
    For x as Integer = 0 to numList.count - 1
        If numList(x) > Big_num_1 Then
            Big_num_2 = Big_num_1
            Big_num_1 = txtBox_1
        End If    
    Next    

End Sub