第九章第四题(使用 Random类)(Using the random class)

第九章第四题(使用 Random类)(Using the random class)

  • *9.4(使用 Random类)编写一个程序,创建种子是 1000 的 Random 对象,然后使用 nextInt(100) 方法显示 0 到 100 之间的 50 个随机整数。
    *9.4(Using the random class)Write a program to create a random object with a seed of 1000, and then use the nextint (100) method to display 50 random integers between 0 and 100.
  • 参考代码:
package chapter09;

import java.util.Random;

public class Code_04 {
    
    
    public static void main(String[] args) {
    
    
        Random random = new Random(1000);
        for (int i = 0, n = 0; i < 50; i++, n++) {
    
    
            if(n % 10 == 0)
                System.out.println("");
            System.out.print(random.nextInt(100) + " ");
		}
	}
}

  • 结果显示:
87 35 76 24 92 49 41 45 64 50 
79 59 72 83 36 75 46 2 23 41 
22 71 89 2 93 42 49 42 35 76 
32 0 52 95 87 31 99 18 79 2 
91 5 55 84 71 95 58 87 77 38 
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109256974