为了生成具有限制的随机数,我们在这里以印度的国家/地区代码为91的号码为例。
首先,我们设置了前两个数字,即国家/地区代码。为每个数字声明变量。我们还将国家/地区代码91后的数字的第一位固定为9:
Random num = new Random(); int num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11; num0 = 9; num1 = 1; num2 = 9;
现在,对于其余数字,请使用nextInt()
随机数的。该参数由随机数的边界集组成:
num3 = num.nextInt(9) + 10; num4 = num.nextInt(10); num5 = num.nextInt(5) + 11; num6 = num.nextInt(10); num7 = num.nextInt(3); num8 = num.nextInt(5); num9 = num.nextInt(10);
import java.util.Random; public class Main { public static void main(String[] args) { Random num = new Random(); int num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11; num0 = 9; num1 = 1; num2 = 9; num3 = num.nextInt(9) + 10; num4 = num.nextInt(10); num5 = num.nextInt(5) + 11; num6 = num.nextInt(10); num7 = num.nextInt(3); num8 = num.nextInt(5); num9 = num.nextInt(10); System.out.print("Random (Country code 91 for India) = "); System.out.print(num0); System.out.print(num1); System.out.print("-" + num2); System.out.print(num3); System.out.print(num4); System.out.print(num5); System.out.print(num6); System.out.print(num7); System.out.print(num8); System.out.print(num9); } }
输出结果
Random (Country code 91 for India) = 91-9114158010