Java 应用Math.random()调用数组
是否可以使用Math.random()命令从数组中调用随机行,或者实际上有其他方法可以实现这一点?对不起,如果这有一个明显的答案,但我是一个全新的编程。我的代码如下:Java 应用Math.random()调用数组,java,android,arrays,random,Java,Android,Arrays,Random,是否可以使用Math.random()命令从数组中调用随机行,或者实际上有其他方法可以实现这一点?对不起,如果这有一个明显的答案,但我是一个全新的编程。我的代码如下: final float line_positions[][] = new float[][] { //X coordinate of line, Y Coordinate of hole } { 600, 0.5f }, { 900, 0.3f }, { 1200,
final float line_positions[][] = new float[][] {
//X coordinate of line, Y Coordinate of hole
}
{ 600, 0.5f },
{ 900, 0.3f },
{ 1200, 0.2f },
{ 1500, 0.3f },
{ 1800, 0.1f },
{ 2100, 0.4f },
{ 2400, 0.5f }
};
......
int temp_score = 0;
for (int i = 0; i < line_positions.length; i++) {
float y = line_positions[i][Y] * ScreenHeight();
float x = ScreenX((int) line_positions[i][X] * ScreenWidth() * 0.0015f);
}
final float line_positions[][]=新浮点[][]{
//线的X坐标,孔的Y坐标
}
{600,0.5f},
{900,0.3f},
{1200,0.2f},
{1500,0.3f},
{1800,0.1f},
{2100,0.4f},
{2400,0.5f}
};
......
int temp_分数=0;
对于(int i=0;i
对其使用Random
Random rnd = new Random();
int randomRow = rnd.nextInt(line_positions.length);
但是,如果您希望每一行只显示一次,那么我建议将数组强制转换为列表,使用
Collections.shuffle()
对列表进行随机和迭代。那么您希望将I
替换为随机数?但仍然检索循环中的所有行位置
?