随机化方法的问题(Issue with randomize method)
我有一个方法无法正常工作。 该方法应该随机地将一组数字从1到20排序(每个数字必须只出现一次)。 我的问题是,当我运行程序时,一些数字会重复多次。 代码如下:
public static int randomize(int index) { //This array will hold the 20 numbers. int[] randomIndex = new int[20]; Random ranNum = new Random(); for (int x = 0; x<20; x++) { int temp; //The number is generated randomly and saved in temp. temp = ranNum.nextInt(20); //This loop skips the first index. if (x != 0){ /*Here, the loop is supposed to compare a generated number with the previous one*/ for (int y = 1; y<=x; y++) { while(temp == randomIndex[x-y] ) { /*If the while loop finds that temp variable matches any previous number it will generate another random number for it until it finds no matches.*/ temp = ranNum.nextInt(20); } } } /*Once no match has been found for temp, the number is assigned to an index, and the loop is executed with a x variable increment. randomIndex[x] = temp; } //Finally the array with the set of random numbers is sent to the main function. return randomIndex[index]; }
我得到以下输出:
19, 19, 5, 16, 6, 2, 18, 1, 15, 1, 5, 19, 11, 4, 18, 0, 5, 18, 10.
所以现在我不知道该怎么做。 :C
I have a method that is not working properly. The method is supposed to sort a set of numbers from 1 to 20 randomly (each number must appear just once). My issue here is that when I run the program, some numbers are repeated several times. The code is the following:
public static int randomize(int index) { //This array will hold the 20 numbers. int[] randomIndex = new int[20]; Random ranNum = new Random(); for (int x = 0; x<20; x++) { int temp; //The number is generated randomly and saved in temp. temp = ranNum.nextInt(20); //This loop skips the first index. if (x != 0){ /*Here, the loop is supposed to compare a generated number with the previous one*/ for (int y = 1; y<=x; y++) { while(temp == randomIndex[x-y] ) { /*If the while loop finds that temp variable matches any previous number it will generate another random number for it until it finds no matches.*/ temp = ranNum.nextInt(20); } } } /*Once no match has been found for temp, the number is assigned to an index, and the loop is executed with a x variable increment. randomIndex[x] = temp; } //Finally the array with the set of random numbers is sent to the main function. return randomIndex[index]; }
And I got the following output:
19, 19, 5, 16, 6, 2, 18, 1, 15, 1, 5, 19, 11, 4, 18, 0, 5, 18, 10.
So now I have no idea what to do. :C
原文:https://stackoverflow.com/questions/21419911
最满意答案
有一个功能VB6类用于执行此操作:
包含示例程序,其中一个是“查看器”,另一个只是将
RT_STRING
资源字符串提取到XML文档。 您可以轻松地更改后者以将字符串值转储到文本文件,数据库等。There is a functional VB6 Class for doing this at:
ResDecomp Class Decompiles RES Files
Sample programs are included, one of them a sort of "viewer" and the other just extracts
RT_STRING
resource strings to an XML document. You could easily change the latter to dump the string values to a text file, database, etc. instead.
相关问答
更多-
VB和C语言哪个难一些?[2023-06-26]
vb -
VB6与VB.NET的差异有哪些?[2022-02-04]
.Net平台引入了很多先进的技术,导致了VB6和VB.Net之间产生了一个巨大的鸿沟 就语言功能上来讲,VB.net完全是为了.Net技术而生,具有更好的运行效率和更加强大的功能(某些方面甚至超过了C#) 从语法角度上来讲,VB.net成为了真正的OOP(面向对象语言),不但支持类的继承、派生,还支持委托、泛型等等更加高级的编程技术 如果你是从VB6升级到.net的话,建议要将VB.net当作一门新的语言来学。以VB6的经验来应用到VB.net上是会犯很多错误的 关于从VB6升级到VB.net的代 ... -
VB6与VB.NET的差异有哪些?[2022-10-11]
.Net平台引入了很多先进的技术,导致了VB6和VB.Net之间产生了一个巨大的鸿沟 就语言功能上来讲,VB.net完全是为了.Net技术而生,具有更好的运行效率和更加强大的功能(某些方面甚至超过了C#) 从语法角度上来讲,VB.net成为了真正的OOP(面向对象语言),不但支持类的继承、派生,还支持委托、泛型等等更加高级的编程技术 如果你是从VB6升级到.net的话,建议要将VB.net当作一门新的语言来学。以VB6的经验来应用到VB.net上是会犯很多错误的 关于从VB6升级到VB.net的代码变换问题 ... -
将字符串表(二进制.res文件)从VB6迁移到VB.NET(Migrating a string table (binary .res file) from VB6 to VB.NET)[2022-10-19]
有一个功能VB6类用于执行此操作: ResDecomp类反编译RES文件 包含示例程序,其中一个是“查看器”,另一个只是将RT_STRING资源字符串提取到XML文档。 您可以轻松地更改后者以将字符串值转储到文本文件,数据库等。 There is a functional VB6 Class for doing this at: ResDecomp Class Decompiles RES Files Sample programs are included, one of them a sort of " ... -
有一个来自vb.net。 当您打开.vbp vb6项目时,它会询问您是否想自动将其转换为vb.net。 它做得不错,不包括第三方附加组件。 There is one that comes with vb.net. When you open a .vbp vb6 project, it asks if you would like to automatically convert it to vb.net. It does a pretty good job, exclusive of third part ...
-
这是在VB6中逐行加载整个文件的最快方法: Function FileText (filename$) As String Dim handle As Integer handle = FreeFile Open filename$ For Input As #handle FileText = Input$(LOF(handle), handle) Close #handle End Function Public Function ReadFileIntoStr ...
-
Open strFileName For Output As #FileHandle% MousePointer = vbHourglass Print #FileHandle%, strText MousePointer = vbDefault Close #FileHandle% 转换为VB.NET代码: Me.Cursor = Cursors.WaitCursor Using fs As New StreamWriter(strFileName, Fa ...
-
触发器是正确的vbNull是一个常量,但它的值是这样定义的: Public Const vbNull As VariantType = VariantType.Null 请参阅Microsoft参考源 Trigger is correct vbNull is a constant, but its value is defined thusly: Public Const vbNull As VariantType = VariantType.Null Please see Microsoft Refere ...
-
作为参考: http : //technet.microsoft.com/en-us/sysinternals/bb897439.aspx Thanks all. Great ideas. Really helped me think. Upvotes all around. Ended up I didn't need to be very sure that they were strings so I went with a quick, sloppy, ugly, hack. 'strip out ...
-
VB6:慢速二进制写入?(VB6: Slow Binary Write?)[2022-09-20]
那么,你是一个接一个读一个字节吗? 在这种情况下,你迭代了200万个元素,而不是一次只取一块数据并将其写入流中。 Well, are you reading and writing each byte 1 by one? In that case you are iterating through 2 million elements instead of just taking a chunk of data at a time and write it to the stream.