所以,正如你所看到的,我尝试在这里设置一些循环,两个 if 语句,我在 java 脚本中使用了嵌套的 for 循环,所以我认为这可能有效,但运气不佳。
观察第三组,我是从1000 - 10000
制作的,所以它将是从1000-9999
开始,因为,我不知道如何制作它从0000-9999
,需要打印那些0
(电话号码)
我是编程新手,所以请对我说得非常清楚和简单,谢谢。
目标
- xxx-xxx-xxxx
- 第一组 = 3 个数字,不能有 8 或 9
- 第二组,不超过 742
- 第三组 = 0000 -9999
import java.util.Random;
import java.lang.Math;
public class Main{
public static void main(String[] args){
Random rand= new Random();
int a1 = rand.nextInt(7);
int a2 = rand.nextInt(7);
int a3 =rand.nextInt(7);
int b = rand.nextInt(741);
int c = rand.nextInt(9999);
while (b.length<4)
{
b (string)= "0"+ c ;
}
while (c.length()<4)
{
c (string)= "0"+ c ;
}
System.out.println(a1 +""+ a2+ ""+ +a3+ "-" + b + "-" + c );
}
}
最佳答案
您可以简单地为a执行此操作:
int array[] = {0, 1, 2, 3, 4, 5, 6, 7};
String st = String.format("%d%d%d", array[(int)(Math.random()*8)] , array[(int)(Math.random()*8)] , array[(int)(Math.random()*8)]);
并在控制台中显示输出:
System.out.println(st + "-" + b + "-" + c );
关于Java整数问题,如何正确设置约束以及设置不允许的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609030/