gpt4 book ai didi

Java Math.random 与数组

转载 作者:行者123 更新时间:2023-12-01 22:35:57 24 4
gpt4 key购买 nike

我正在尝试使用 2d 数组中的随机坐标中的字符(例如 A)填充 5 x 5 的 2d 数组。当我使用嵌套 for 循环时,我想使 char 所在的二维数组的坐标是随机的。假设我要求 40% 的 A,在 5 x 5 中,我应该得到 10 个 A,但我得到了 8 个。当我运行它时,它有时不会显示我想要的 A 的百分比。它只会打印出像 6 这样的结果。这是因为当 if 语句中的 row 和 col 是随机的时,for 循环中的 row 和 col 也是随机的吗?这就是为什么 char 有时填充的数量少于要求的数量,因为如果数字随机化 2d 数组的长度(即 5),for 循环就会停止?

此外,当它打印出 10 个字符时,有时它们会超过 5 × 5。例如,该行中为 2 个 As,第二行中为 7,第三行中为 1。这是为什么?

public static void grid(char[][] arr, int percentofchar)
{

double charx = 0.0;

double totalpercentchar = 0;
totalpercentchar = (double) percentofchar / 100;

cellx = totalpercentchar * (arr.length * arr.length);

for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
if (charx > 0)
{
row = (int) (Math.random() * arr.length);
col = (int) (Math.random() * arr.length);
charx--;
arr[row][col] = 'A';
System.out.print(arr[row][col] + " ");
}
}
System.out.println();
}
}

最佳答案

你的代码应该是这样的

public static void grid(char[][] array, int percentOfChar)
{
int charsToPlace = (int) (percentOfChar * array.length * array.length / 100.0);
while (charsToPlace > 0)
{
int row = (int) (Math.random() * array.length);
int column = (int) (Math.random() * array.length);
if (array[row][column] != 'A');
{
array[row][column] = 'A';
charsToPlace--;
System.out.print(array[row][column] + " ");
}
}
System.out.println();
}

如果您只是尝试在随机位置插入字符,则无需循环遍历数组并使用嵌套循环。

还有

Is this because when the row and col in the if statement are randomized, so is the row and col in the for loop? Is this why the char sometimes populates less then asked for because the for loop stops if the number randomizes the length of the 2d array which is 5? Also when it does print out 10 char, sometimes they go over the 5 by 5. an example would be 2 As in the line and 7 in the second and 1 in the 3rd. Why is that?

或多或少。您可以随机化行和列,但这样做可能会导致数组迭代过早结束。作为最坏的情况,请考虑如果第一次输入 if 语句时随机函数将 4 个值同时分配给 rowcol 会发生什么情况>。一般来说,您确定在grid方法的末尾charx将始终等于0吗?

<小时/>

注意事项

正如 Matt 在下面的评论中指出的那样,此方法不对数组进行检查;因此,它假设数组始终是平方数组(即行 X 列 = n X n)。如果您想强制使用方阵,您可能需要创建一个包装类,例如

class IntegerSquareArray
{
public final int length;
int[][] array;
IntegerSquareArray(int length)
{
this.length = length;
this.array = new int[length][length];
}

public int getValue(int row, int column)
{
if (row < length && column < length)
return array[row][column];
throw new IllegalArgumentException();
}

public void setValue(int row, int column, int value)
{
if (row < length && column < length)
array[row][column] = value;
else throw new IllegalArgumentException();
}
}

然后,您只需将grid代码更改为

public static void grid3(IntegerSquareArray integerSquareArray,
int percentOfChar)
{
int charsToPlace = (int) (percentOfChar * integerSquareArray.length
* integerSquareArray.length / 100.0);
while (charsToPlace > 0)
{
int row = (int) (Math.random() * integerSquareArray.length);
int column = (int) (Math.random() * integerSquareArray.length);
if (integerSquareArray.getValue(row, column) != 'A')
{
integerSquareArray.setValue(row, column, 'A');
charsToPlace--;
System.out.print(integerSquareArray.getValue(row, column) + " ");
}
}
System.out.println();
}

关于Java Math.random 与数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26858352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com