Sim*_*son 6 java double int casting
private void buildingComposotion ()
{
for (int i=1; i<=randomInteger(3,6); i++)
{
int numberOfStories = 2;
buildingFrame(numberOfStories);
buildingWindows(numberOfStories);
}
}
private int randomInteger(int min, int max)
{
min = (int) Math.ceil(min);
max = (int) Math.floor(max);
return Math.floor(Math.random() * (max - min));
}
Run Code Online (Sandbox Code Playgroud)
错误“不兼容的类型:从 double 到 int 可能有损转换”。是15号线。
Math.floor返回 a double,而不是int。修改你的randomInteger方法:
private int randomInteger(int min, int max)
{
min = (int) Math.ceil(min);
max = (int) Math.floor(max);
return (int) Math.floor(Math.random() * (max - min));
}
Run Code Online (Sandbox Code Playgroud)
或者,nextInt从Random类中使用