java返回一个 low 到 high的随机整数 (包括low和high).

java返回一个 low 到 high的随机整数 (包括low和high).
Which /* expression */ will always return a value that satisfies the postcondition?( )
(A) (int) (Math.random() * high) + low;
(B) (int) (Math.random() * (high - low)) + low;
(C) (int) (Math.random() * (high - low + 1)) + low;
(D) (int) (Math.random() * (high + low)) + low;
乌有先生_ 1年前 已收到1个回答 举报

stvswh 幼苗

共回答了16个问题采纳率:93.8% 举报

public int getRandom(int low,int high){
Random r=new Random();
int rand=r.nextInt(high-low+1);//产生一个0到high-low的整数
return rand+low;
}

1年前

10
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.642 s. - webmaster@yulucn.com