java - 在java中的1到50之间的for循环中生成10个随机数
问题描述
我需要在 Java 中创建一个程序,它生成 10 个 1 到 50 之间的随机数并使用for
循环输出它们。我已经想出了如何生成随机数,但无法弄清楚如何使用 for 循环来做到这一点。请帮忙!
import java.util.Random;
class RandomNumbers
{
public static void main (String [] args)
{
int random = (int)(Math.random()* (50 + 1));
System.out.println (random);
}
}
解决方案
Just put that code in a for loop like this:
for(int i=0;i<10;i++){
int random = (int)(Math.random()* (50 + 1));
System.out.println (random);
}
推荐阅读
- testing - 登录到应用程序后的 TestCafe 测试引发错误 401 未经授权
- ruby-on-rails - Rails 路由到未知页面与 ActiveStorage 图像路径混淆
- java - Travis CI - Travis(401 和 400)测试失败,但在本地运行 Java Spring Boot
- apache - Can't Apache2 start errr: 找不到 SOAP/Transport/HTTP.pm
- javascript - 加载文档时,如何阻止 Safari 将注意力集中在标有用户名或密码的表单字段上?
- scrapy-splash - 飞溅经常以状态码 520 下降
- html - 如何将单选按钮值作为数字获取
- c# - 在每个用户输入中都有一个控制台命令
- python - 为什么代码不会写在记事本文件中?
- r - 网格之间的多元线性回归:回顾假设