我最初用 3 个变量编写了这个程序,每组数字一个,但是,我无法让 java 为最后四个数字打印 0007 之类的数字。它只会打印 XXX-XXX-7 而不是 XXX-XXX-0007。如何让随机数生成器为我的电话号码打印额外的 0,例如 0748、0023、0005?谢谢!
import java.util.Random;
public class PhoneNumbers
{
public static void main (String[] args)
{
int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8, digit9, digit10;
Random generator = new Random();
//creates a random number
digit1 = generator.nextInt(8);
digit2 = generator.nextInt(8);
digit3 = generator.nextInt(8);
digit4 = generator.nextInt(8);
digit5 = generator.nextInt(5);
digit6 = generator.nextInt(3);
digit7 = generator.nextInt(10);
digit8 = generator.nextInt(10);
digit9 = generator.nextInt(10);
digit10 = generator.nextInt(10);
//outputs the number including dashes
System.out.println("A random 10-digit phone number:");
System.out.print(digit1);
System.out.print(digit2);
System.out.print(digit3);
System.out.print("-");
System.out.print(digit4);
System.out.print(digit5);
System.out.print(digit6);
System.out.print("-");
System.out.print(digit7);
System.out.print(digit8);
System.out.print(digit9);
System.out.print(digit10);
}
}