简体   繁体   English

如何找到3个整数之间的最大整数

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

I would like to find the largest integer(s) between 3 integers. 我想找到3个整数之间的最大整数。

I could do this by nesting If statements. 我可以通过嵌套If语句来做到这一点。 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 ). 我想知道是否有一种更简单的方法来找到最大整数(包括假设AB相等但都大于C )。

PS Can you do this with 2-D arrays? PS,您可以使用二维阵列吗?

Use LINQ to do this: 使用LINQ可以做到这一点:

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. 根据与OP的对话进行编辑,希望了解哪种流派排名最高。

When asked How do you get the data? 当被问到如何获取数据? OP responds with: OP回应:

I have a text file containing movie|genre on every line. 我的每一行都有一个包含movie | genre的文本文件。 I read this and count which genre (out of 3) is the highest. 我读了此书,然后算出哪一类(最高3种)。

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 . 从上面的代码中,您可以看到正在使用新Film填充的Films类。 I then call a method from the Films class, but only if there are films to choose from. 然后,我从Films类中调用一个方法,但前提是要选择电影。 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. 我必须注意,尽管此代码确实有效,但如果您具有2种或更多类型的联合排行榜,则可能会出现问题。 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 . 将数字放入数组中,然后使用For[...]Next语句遍历数组,将当前成员与max进行比较。 If max is lower, set it to the current member. 如果max较低,则将其设置为当前成员。 When the loop terminates, max will contain the highest number: 循环终止时, max将包含最大数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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