假设我想从两个数字 1
中随机选择和3
.
我该如何去做呢?我只需分配 int a
至1
, int b
至3
,然后从 a
中随机选择和b
?
最佳答案
如果只有两个数字可供选择,那么您可以使用 boolean
值,因为它返回 true
或 false
.
假设 int a = 1
和 int b = 3
的单行解决方案:
int randomOfTwoInts = new Random().nextBoolean() ? a : b;
关于java - 如何从两个整数中随机选择一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311442/