多维数组排序 [英] Ordering Multidimensional arrays

查看:30
本文介绍了多维数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对多维数组进行排序,但不确定这是否是正确的方法.到目前为止,我在多维数组中获取了 5 个数字,然后将它们移动到一维数组中并使用数组排序.知道更好的方法吗?或者有关于如何改进我的想法?此外,该代码当前在排序区域中不起作用,它给了我一个数组错误的索引.

I am trying to sort a multidimensional array, but am not sure if this is the correct way to go about it. So far, I am getting my 5 numbers in the multidimensional array and then moving them into a single dimensional array and using array sort. Does know a better way ? or have ideas on how to improve mine? Also , the code currently isn't working in the sorting area, it gives me an index out of array error.

任何帮助将不胜感激.提前致谢

Any help will be appreciated. Thanks in advance

Module q
    Sub Main()
        Randomize()
        Dim Player,RandomNumber,NumberOfPlayers,Index As Integer
        Dim Roll as Integer = 0
        Console.Write("How many people will be playing Yahtzed?: ")
        Player = convert.toint32(Console.Readline)
        NumberOfPlayers = Player
        Dim Game(Player,5) As Integer
        Do until Player = 0
            Console.Write("User")
            Roll = 0
            Do until Roll = 5
                RandomNumber = CINT(Int((6 * Rnd()) + 1))
                Game(Player,Roll) = RandomNumber
                Roll += 1
                Console.Write(" "&RandomNumber)
            Loop 
            Player -= 1
            Console.Writeline()
        Loop 
        Player = NumberOfPlayers
        Do until Player = 0
            Dim Ordering(5) as Integer
            Roll = 0
            Do until Roll = 5
                Ordering(Index) = Game(Player,Roll)
                Roll += 1
                Index += 1
                Array.Sort(Ordering)
            Loop
        Loop
    End Sub
End Module

推荐答案

迭代数组的惯用方式是使用 For 语句完成的

The idiomatic way of iterating arrays is done using a For-statement

Const  NPlayers As Integer = 5, NRolls As Integer = 5

Dim game = New Integer(NPlayers - 1, NRolls - 1) {}
Dim randomizer As New Random()
Dim randomNumber As Integer

For p As Integer = NPlayers - 1 To 0 Step -1
    For r As Integer = 0 To NRolls - 1
        randomNumber = randomizer.[Next](10)
        game(p, r) = randomNumber
    Next 
Next

数组索引总是从 0arraysize-1.

The array indexes always go from 0 to arraysize-1.

排序数组的大小为 NPlayers * NRolls

Dim Ordering(NPlayers * NRolls - 1) As Integer

你的太短了.因此你会得到一个例外.

Yours is too short. Therefore you get an exception.

可以使用 Random 类创建整数随机数

Integer random numbers can be created using the Random class

Dim randomizer As New Random()
Dim randomNumber As Integer

...

randomNumber = randomizer.[Next](10)

这会创建 0 到 10 之间的随机数.只创建一次随机数,然后通过调用 Next 方法获取下一个随机数.

This creates random numbers between 0 and 10. Create the randomizer only once and then get the next random number by calling the Next method.

(必须将Next方法放在方括号内,因为Next是VB中的关键字.)

(You must put the Next method between brackets, because Next is a keyword in VB.)

更新

我假设您想对每个玩家的掷骰子进行排序.实现此目的最简单的方法是使用锯齿状数组.

I assume that you want to sort the rolls per player. The easierst way to accomplish this is to use a jagged array instead.

Const  NPlayers As Integer = 5, NRolls As Integer = 5

Dim game = New Integer(NPlayers - 1)() {}
For p As Integer = NPlayers - 1 To 0 Step -1
    game(p) = New Integer(NRolls - 1) {}
    For r As Integer = 0 To NRolls - 1
        game(p)(r) = randomizer.[Next]()
    Next
    Array.Sort(game(p))
Next

但是您必须使用 game(p)(r) 而不是 game(p,r) 来访问卷.

But the you will have to access the rolls with game(p)(r) instead of game(p,r).

这篇关于多维数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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