索引超出数组范围? [英] Index out of bounds of array?

查看:26
本文介绍了索引超出数组范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,它将 15 个随机生成的数字相互比较并输出最大的数字.我已经完成了大部分工作,但我收到一条错误消息,提示索引超出数组范围".我需要变量 nextint 作为 currentint 之后的下一个索引,因此 + 1 以便将它们相互比较.我会把代码放在下面.

I am trying to make a program where it compares 15 randomly generated numbers to each other and outputs the largest one. I have done most of it but i get an error saying 'index was out of bounds of array'. I need the variable nextint to be the next index after currentint, hence the + 1 in order to compare them to each other. I will put the code below.

        Dim largest As Integer
        Dim random As New Random()
        Dim a, b, c, d, e, f, g, h, i, j, k, l, m, n, o As Integer
        Dim array1 As Array = New Integer() {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o}
        Dim index As Integer = array1.Length

        Console.WriteLine("This program will generate 15 numbers and print the largest of them.")



        For a = 0 To array1.Length - 1

            Dim currentint As Integer = array1(index)
            Dim nextint As Integer = array1(index + 1)

            currentint = random.Next(0, 100)
            nextint = random.Next(0, 100)

            Console.WriteLine(currentint)

            If currentint > nextint Then
                largest = currentint
            ElseIf nextint > currentint Then
                largest = nextint
            End If

        Next

        Console.WriteLine("The largest number is " & largest)

        Console.ReadLine()

推荐答案

您想生成 15 个随机数并显示其中的最大数目吗?

Dim random As New Random()
Dim listRandomNumbers As New List(Of Integer)

For nIndex As Integer = 1 To 15
    listRandomNumbers.Add(random.Next(0, 100))
Next

listRandomNumbers.Sort()

Dim smallestNumber As Integer = listRandomNumbers(0)
Dim largestNumber As Integer = listRandomNumbers(listRandomNumber.Count - 1)

<小时>

你的问题在下面一行:


The issue of your question is in the following line:

Dim nextint As Integer = array1(index + 1)

如果indexarray1.Length - 1,这是不可能的.因此,您可以使用以下代码跳过循环的最后一轮:

This is not possible in case index is array1.Length - 1. So you can skip the last round of the loop with the following code:

If index = array.Length - 1
    Continue For
End If

For 循环的新代码如下所示:

The new code of the For loop looks like this:

For a = 0 To array1.Length - 1

    If index = array.Length - 1
        Continue For
    End If

    Dim currentint As Integer = array1(index)
    Dim nextint As Integer = array1(index + 1)

    currentint = random.Next(0, 100)
    nextint = random.Next(0, 100)

    Console.WriteLine(currentint)

    If currentint > nextint Then
        largest = currentint
    ElseIf nextint > currentint Then
        largest = nextint
    End If
Next

您还可以使用随机整数 (random.Next(0, 100)) 覆盖 nextintcurrentint.不使用变量的初始值.

You also overwrite the nextint and currentint with a random integer (random.Next(0, 100)). The init values of the variables are not used.

您可以使用排序函数代替 For 循环来获取数组的最大数.有一个更简单的解决方案,使用 Array.Sort:

You can use a sort function instead of a For loop to get the largest number of the array. There is a simpler solution using Array.Sort:

Dim a, b, c, d, e, f, g, h, i, j, k, l, m, n, o As Integer
Dim array1 As Array = New Integer() {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o}

Array.Sort(array1)

'largest and smallest number.
Dim largestNumber As Integer = array1(array1.Length - 1)
Dim smallestNumber As Integer = array1(0)

这篇关于索引超出数组范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆