2016-10-27 24 views
0
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)); 
} 

错误,“不兼容的类型:可能有损从双转换为整数。”在线15上。不兼容的类型:可能有损于从double到int的有损转换。我不知道为什么我得到这个错误信息

+1

没有必要为你调用'Math.ceil(分钟)'或'Math.floor(最大值)'。这些值已经是整数。您还需要将'min'添加到返回的值。 – 4castle

回答

0

Math.floor返回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)); 
} 

或者,使用nextIntRandom

+0

神奇,感谢您的快速响应!这是我第一次使用stack over flow,我从来没有料到过会这么快速回复。 干杯! –

相关问题