简体   繁体   English

JAVA如何生成1到24之间的整数,但每个整数都需要一对

[英]JAVA How do I generate integers between 1 and 24 but I need a pair of each integer

I'm making the memory game concentration. 我正在集中记忆游戏。 In this I have a specified obligation to generate random pairs of numbers. 在这种情况下,我有义务生成随机数对。 These are to be assigned to a cell at its creation on a game board. 这些将在游戏板上创建单元时分配给该单元。 My problem lies not in how to generate a random number as I am able to do this with -- 我的问题不在于如何生成随机数,因为我可以使用-

type = generator.nextInt(NUMBER_OF_ROWS*NUMBER_OF_COLUMNS);

However I must make sure that when I am populating my game board with Cell objects 但是,我必须确保在用Cell对象填充游戏板时

board[row][column] = new Cell(this,type, row, column);

I have 2 cells of the same type, in order for the game to function. 为了让游戏正常运行,我有2个相同类型的单元格。 Basically I need 12 pairs of numbers that have been randomly generated. 基本上我需要随机生成的12对数字。

Thanks 谢谢

Varun 瓦伦

Invert the problem. 解决问题。 Rather than think of generating 24 random numbers, what you really want is to distribute the numbers 1-12 randomly into a 24-cell array, with each number going into TWO randomly selected cells in the array. 与其想生成24个随机数,不如真正想要的是将数字1-12随机分配到24个单元格的数组中,每个数字都放入数组中随机选择的两个单元格中。 The simplest way to do this would be be to create a List<Cell> , populate it with 24 cells (two containing value 1, two containing value 2, etc), and then use Collections.shuffle(List<?>...) 最简单的方法是创建一个List<Cell> ,用24个单元格填充(两个包含值1,两个包含值2,依此类推),然后使用Collections.shuffle(List<?>...)

The question is not clear. 问题尚不清楚。 But I think you want to have two cells of same type ie you have 24 cells and you want to make 12 pairs of them. 但是我想您要有两个相同类型的电池,即您有24个电池,并且要制作12对。 If you want the cells to be random you can generate two random rows number and two random column number and pair them. 如果希望单元格是随机的,则可以生成两个随机的行号和两个随机的列号,并将它们配对。 You have to look that the pair is not repeated. 您必须确保该对不会重复。

EDIT: No function can generate pair of numbers. 编辑:没有函数可以生成一对数字。 What you can do is number the cells in 2-D array , like suppose the row count is 3 and columns is 2 then number it like 0,1,2,3,4,5. 您可以对二维数组中的单元进行编号,例如假设行数为3,列为2,然后对其进行编号,例如0,1,2,3,4,5。 Now start generating random numbers and set the upper limit to 5. Suppose the first no. 现在开始生成随机数,并将上限设置为5。假设第一个数字为0。 generated is 2 and second one is 5 then you can call it a pair. 生成的是2,第二个是5,那么您可以将其称为对。 Then again generate the numbers. 然后再次生成数字。 Next pair can 1 and 0 and so on. 下一对可以为1和0,依此类推。

It looks like you are not familiar with 2 Dimensional arrays or my understanding of the question is wrong. 您似乎不熟悉2维数组,或者我对该问题的理解是错误的。 I think the game requires the player to remember a pair of cells. 我认为游戏要求玩家记住一对细胞。 Totally there are 12 pairs so have a 2D array with one index varying from 0 to 11 which denotes pair number and another index which distinguishes the two cell numbers in a pair. 总共有12对,因此有一个2D数组,其中一个索引从0到11变化,表示对号,另一个索引区分对中的两个单元格号。

You need to store in an integer array called pairtype[12][2] two numbers which are paired. 您需要将两个成对的数字存储在一个称为pairtype [12] [2]的整数数组中。 Say suppose cell no 1 is paired with 15 then there will be one value {1,15} in the array pairtype. 假设第一个单元格与15配对,那么数组对类型中将有一个值{1,15}。 So you can store 12 pairs of two integers in the array pairtype. 因此,您可以在数组对类型中存储12对的两个整数。

What mistake you are doing is using type which can range from 0 to 23 to assign to a cell type. 您正在执行的错误是使用类型(可以介于0到23之间)分配给单元格类型。 Instead use the first index(index1) of pairtype[index1][index2] as cell type which will then vary from 0 to 11. 相反,请使用pairtype [index1] [index2]的第一个索引(index1)作为单元格类型,其范围从0到11。

You also need to take care not to generate pairs like {1,15} and {1,12} in the array pairtype. 您还需要注意不要在数组对类型中生成像{1,15}和{1,12}这样的对。

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

相关问题 如何在Java中生成min和max之间的随机整数? - How do I generate a random integer between min and max in Java? 如何在JAVA中生成RSA密钥对(openssl格式) - How do I generate RSA key pair in JAVA (in openssl format) 如何在 Java 中生成特定范围内的随机整数? - How do I generate random integers within a specific range in Java? 如何在Java中生成随机整数? - How can I generate random integers in java? 在 Java 中,我需要用户输入五个整数然后显示平均值。 如何在不输入第 6 个条目的情况下获得总和中包含的第 5 个整数? - In Java, I need the user to enter five integers then show the average. How do I get the 5th integer included in the sum without typing a 6th entry? 在Java中,如何将每个字符设置为整数? - In Java, how do I set each character as an integer? Java:如何在Java中使用整数对? - Java: How can I use an integer pair in Java? 如何使用Java生成1到6之间的6个随机数? - How do I generate 6 random numbers between 1 and 6 using Java? 当放置受到限制时,如何生成 java 中的所有整数组合? - How do I generate all combinations of integers in java when placement is restricted? 每当用户输入字符串值时,如何自动生成整数值? - How do I auto-generate an integer value for each time a user input a string value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM