Random方法:生成指定长度的随机数字

import java.util.Random;
import java.util.Scanner;

public class HomeWork_02 {

	public static void main(String[] args) {
		run();
	}

	public static void run() {
		show("请输入一个长度:");
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		char[] cs = new char[n];
		Random r = new Random();
		for (int i = 0; i < n; i++) {
			show( r.nextInt(9) );//生成随机数
		}
	}

	public static void show(Object obj) {
		System.out.print(obj);
	}
}

你可能感兴趣的:(java代码库)