gpt4 book ai didi

Java:生成 "00"作为可能的 Math.random 输出

转载 作者:行者123 更新时间:2023-11-30 05:25:56 25 4
gpt4 key购买 nike

我正在为编程课作业创建一个“轮盘赌轮”。该轮使用 Math.random 生成 0 到 36 之间的随机数。作业的一部分是添加“双零”(00) 作为可能的输出。通过这个等式可以做到吗?

     spin = (int) (Math.random()*(37 - 0)) + 0;

最佳答案

假设您希望 000 成为单独的输出,则需要获取 String,因为整数会处理这两个作为相同的值。我想到的一个选项是使用第 39 个可能的输出。您可以将其添加到下面的代码中:

String getSpin() {
int spin = (int)(Math.random() * 38);
if (spin == 38) return "00";
else return Integer.toString(spin);
}

关于Java:生成 "00"作为可能的 Math.random 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com