首页 > 解决方案 > 在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);
}
}

标签: javafor-looprandom

解决方案


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);
}

推荐阅读