我正在使用IntelliJ Idea进行编码课程,我决定制作一个有趣的项目。这是我的代码。请注意,java.util.Random已导入:
<cfset myarray=ArrayNew(1)>
<cfset counter = 1>
<cfloop list="#url.TableName#" index="y">
<cfquery name="findgreatest" dbtype ="query">
SELECT max(arn) as recno from qSort
WHERE tbl = '#y#'
</cfquery>
<cfset nextTBRC[counter] = ["#findgreatest.recno#"]>
<cfset counter = counter + 1>
</cfloop>
<cfoutput>
<cfset myvar = "#nextTBRC[2]#">
<div>#myvar#</div>
</cfoutput>
printTrees只是以您在屏幕截图中看到的方式打印出数组输入。 输出通常如下所示:
我的代码应该基本上生成一种森林,树木是Ts,下半部分应该是镜像上半部分。然而,下半部分完全是Ts。我如何解决这个问题,以便下半部分是与上半部相似的模式随机,只能逆转?
答案 0 :(得分:3)
正如评论中的@SeanVanGorder所示,您经常点击IllegalArgumentException
个案,因此您总是为“随机”值选择零。 (Sounds like an xkcd comic)。
try / catch块中唯一会抛出此异常的是对Random.nextInt(int)
的调用,如果参数不为正,则documented抛出IllegalArgumentException
。因此,您的参数Math.abs((int) Math.round(randBounds))
不是正面的。
它不是正面的原因是它变为零。很容易理解为什么:
randBounds
循环的所有50次迭代后,for(int q = 0;q<50;q++){...}
的值为150 / 1.07 ^ 26*1.07^24 = 150 * 0.873 = 131
randBounds
除以1.05,使其变为150 * 0.873 / 1.05 = 150 * 0.831 = 124.7
,并再次重复这些分区/多重放映26次(外循环的迭代)。在这26次迭代结束时,randBounds
仅为1.25。然后重复内循环(使randBounds
每次迭代的大小为0.873倍,但现在乘以 1.05而不是除以1.05。但整体变化每次迭代到randBounds
为0.873 * 1.05 = 0.917
,即你的数量仍在减小。
从1.25开始并将幅度减小0.917倍,经过10次迭代后你将降到0.5以下。因此,一旦完成外循环的36次迭代,randBounds
小于1,您将始终获得IllegalArgumentException
。
然而,一旦0.5 <= randBounds < 1.5
开始,你就会开始变为零,因为它会变为1,而Random.nextInt(1)
总是会返回零。所以,实际上,你将开始比36迭代标记更早得到所有零。实际上,这发生在外循环的25次迭代之后。
很难建议修复,因为不清楚正确的行为是什么;唯一的一般修复是识别未检查的异常(如IllegalArgumentException
)不应被捕获和吞噬,因为它们表示编程错误。
答案 1 :(得分:0)
或许高斯分布会更符合您的喜好:
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import java.util.Random;
public class StackOverflow45044823 {
public static void populateField(final char[][] field) {
Validate.noNullElements(field);
final Random random = new Random();
for (int rowNumber = 0; rowNumber < field.length; rowNumber++) {
for (int columnNumber = 0; columnNumber < field[rowNumber].length; columnNumber++) {
//random.nextGaussian() / 6.0 = stdev=1/6 center=0
//rowNumber / (float) field.length - 0.5 = grows uniformly from -0.5 to 0.5
if (random.nextGaussian() / 6.0 < rowNumber / (float) field.length - 0.5) {
field[rowNumber][columnNumber] = 'T';
} else {
field[rowNumber][columnNumber] = ' ';
}
}
}
}
public static void main(String[] args) {
final char[][] treeField = new char[50][50];
populateField(treeField);
printField(treeField);
}
private static void printField(char[][] field) {
Validate.noNullElements(field);
for (char[] aField : field) {
System.out.println(StringUtils.join(aField, '|'));
}
}
}
它会产生类似......
的东西 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | |T| | | | | | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | |T| | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | |T| | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| |T| | | | | | | |
|T| | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T|T|
| | | | | | | | | | | | | | | |T| | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | |T| |
| | | | | | | | | | | |T| | | | |T| | | | | |T| |T| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | |T|T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | |
| | | | | | | | | | | | | | | | | | |T| | | | |T| | | | | | | | | |T| | | | |T| | | | | |T| | | |
| | | | | |T| | | | | | |T|T| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | |T|T|T| | | | |T| |T| | | | | | | | | | | | | | | | | | | | | | | | |T| | | | | |
| | | | |T| | | | |T| | |T| | | | | | |T| | | | |T|T|T| | | | | | | |T| | | | | |T| | | |T| | | |
| |T| | | |T| | | | | | | | |T| | | |T| | | | | | |T|T|T|T| | | | |T| | |T| | | | | | |T| | | | |T
T|T| | | | | | | | |T| | | | |T| | |T| | | | |T|T|T| | | | | | | | | | | |T| | | |T| |T| | | | | |
| | |T| |T| | | | | |T| | | | | | | | |T| | | | | |T| |T|T| | | |T| | | | |T| | | |T| | |T| |T| |
T| | |T|T|T|T| |T|T|T| |T| | |T| |T| |T|T| | |T|T| | | |T|T| |T|T| |T|T|T| | |T| | |T|T| |T| | | |T
T| |T|T| | | |T|T| | | | | |T|T| | | |T| | | | | |T|T|T| |T| |T| | |T|T|T| |T|T|T|T| | | | | |T| |
T| | | |T|T| | |T|T| |T| | | | | |T|T|T|T|T| | | |T|T| | | | | | |T|T| | | | | |T| | | |T|T| | | |T
T|T| | |T| |T| | |T| | |T| | | | | |T| |T|T| | | |T|T|T| |T|T|T| | |T| |T| |T| | | | | |T|T|T|T| |T
| |T| |T|T|T|T| |T| |T|T| |T|T| |T| | |T|T|T|T| |T| |T| |T| |T| |T|T|T| |T| | |T|T| |T| |T| |T|T|
| |T| | | |T|T|T| |T| |T| |T| |T| |T|T| |T| |T|T| | |T|T|T|T| | | | |T|T|T|T|T| |T| |T|T|T| |T|T|
|T|T|T| |T|T| | | |T|T|T| |T|T|T| |T|T| | |T|T| | | |T| | |T|T| |T|T|T|T| | | |T|T| |T|T|T| |T| |T
T|T|T| |T|T| |T| |T| | |T|T|T|T| |T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T| |T| |T|T|T|T| |T|T
T|T|T|T|T| |T|T|T|T|T| | |T|T|T| |T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T| |T| |T|T| |T|T|T|T| |T|T
T|T|T|T|T|T| |T|T|T| |T| |T|T| |T|T|T|T|T|T| | |T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T| |T| |T|T|T|T|T|T
T|T|T|T|T| |T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T| |T|T|T|T|T| | |T|T|T|T|T| |T| |T|T|T|T|
T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T| |T|T|T|T|T|T|T| |T|T| |T|T|T|T|T|T| | |T
T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T| |T|T| |T|T| |T|T
T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T| |T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T| |T|T| | |T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T|T| |T|T|T|T|T|T|T|T
T|T|T|T|T|T|T|T|T|T|T| |T|T|T| |T|T|T|T|T|T|T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