我需要随机生成50个整数,范围在1到100之间。需要用什么方法,random()和nextInt()有什么区别吗?

解决方案 »

  1.   

    public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
    System.out.print((int)(Math.random()*100)+"、");
    }
      

  2.   

    for (int i = 0; i < 50; i++) {
    Random rd = new Random();
    System.out.print(rd.nextInt(100)+"、");
    }
    Random 是类,而nextInt()是它的方法!
      

  3.   

    random返回是0到1之间的数,是float的
    nextInt可以设置范围
    Random.nextInt(100)就是0到100的整数
      

  4.   

    用Random.nextInt(100)是生成的不同的随机数吗
      

  5.   

    恩, random可以生成实例, 然后用nextInt获得整数。Math.rando() return double.
      

  6.   


    import java.util.*;public class Test 
    {
    public static void main(String[] args)
    {
    int[] numbers = new int[50];

    //使用Math.random()
    for (int i = 0; i < numbers.length; i++)
    numbers[i] = (int) (Math.random() * 100) + 1;
    for (int i = 0; i < numbers.length; i++)
    System.out.print(numbers[i] + " ");
    System.out.println();

    //使用nextInt()
    Random random = new Random();
    for (int i = 0; i < numbers.length; i++)
    numbers[i] = random.nextInt(100) + 1;
    for (int i = 0; i < numbers.length; i++)
    System.out.print(numbers[i] + " ");
    System.out.println();
    }
    }
      

  7.   

    random是Math的一个方法 返回0-1的float数 而Random是一个类 nextInt()是他的一个方法 返回一个整数