2016-10-27 32 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,我從來沒有料到過會這麼快速回復。 乾杯! –

相關問題