简体   繁体   中英

How to find the largest integer(s) between 3 integers

I would like to find the largest integer(s) between 3 integers.

I could do this by nesting If statements. Since I have further code to write however this would be long and untidy.

I was wondering if there was an easier way to find the largest integer(s) (including if let's say A and B are equal but both higher than C ).

PS Can you do this with 2-D arrays?

Use LINQ to do this:

Dim numbers() As Integer = {1, 3, 5}

Dim max As Integer = numbers.Max()

Debug.Write("Max number in numbers() is " & max.ToString())

Output:

在此处输入图片说明

Edited as per conversation with OP on wanting to know which genre was ranked the best.

When asked How do you get the data? OP responds with:

I have a text file containing movie|genre on every line. I read this and count which genre (out of 3) is the highest.

I have drafted up some code which reads from a text file and populates a class.

First let me show you the code:

Dim myFilms As New Films

Using sr As New IO.StreamReader("C:\films.txt")

    Do Until sr.Peek = -1

        Dim columns As String() = sr.ReadLine().Split(New Char() {"|"c}, StringSplitOptions.RemoveEmptyEntries)

        'columns(0) = film name
        'columns(1) = genre
        myFilms.Add(New Film(columns(0), columns(1)))
    Loop

End Using

If myFilms.Count > 0 Then
    Dim bestGenre = myFilms.GetBestGenre()

    'Go off and read the genre file based on bestGenre
End If

From the above code you can see the class Films being populated with a new Film . I then call a method from the Films class, but only if there are films to choose from. Let me show you the class structure for both these:

Film:

Public Class Film
    Public Key As String

    Public Sub New(ByVal filmName As String,
                   ByVal genre As String)

        _filmName = filmName
        _genre = genre

    End Sub

    Private _filmName As String
    Public ReadOnly Property FilmName As String
        Get
            Return _filmName
        End Get
    End Property

    Private _genre As String
    Public ReadOnly Property Genre As String
        Get
            Return _genre
        End Get
    End Property

End Class

Films:

Public Class Films
    Inherits KeyedCollection(Of String, Film)

    Protected Overrides Function GetKeyForItem(ByVal item As Film) As String
        Return item.Key
    End Function

    Public Function GetBestGenre() As String

        Return Me.GroupBy(Function(r) r.Genre).OrderByDescending(Function(g) g.Count()).First().Key

    End Function

End Class

I must note that although this code does work it may come unstuck if you have 2 or more genres which are joint top. The code still works however it only returns one of the genres. You may want to expand on the code to suit your needs based on that scenario.

Try something like this:

Dim max As Integer
max = integer1

If integer2 > max Then
   max = integer2
End If

If integer3 > max Then
   max = integer3
End If 

Not many more ways that I can think of off the top of my head to do this.

Something along these lines will work for any number of integers. Put the numbers into an array then use a For[...]Next statement to loop through the array comparing the current member with max . If max is lower, set it to the current member. When the loop terminates, max will contain the highest number:

Dim nums() As Integer = {1, 2, 3}
Dim max As Integer

For i = 0 To nums.Length - 1
    If max < nums(i) Then
        max = nums(i)
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM