面试手写代码的题目

  • 提取出来的场景模拟:对于1–16,调用一次方法,就让他产生4个数字,调用4次完毕,产生4组不同的数字!
  • 注意:nextInt(101)是产生0-100之间的任意整数,不包括101.
    *
    完整代码:

    public class World {
    static int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    public static void random(){
    System.out.println(“——————–”);
    int count=0;
    Random random = new Random();
    while (true) {
    int index = random.nextInt(16);//产生下标为0-15之间!
    if (a[index] != 0) {
    count++;
    System.out.print(a[index]+” “);
    a[index] = 0;
    if (count == 4)
    break;
    }
    }
    System.out.println();

    }

    public static void main(String [] args){
    for(int i=1;i<=4;i++){
    random();
    }
    }
    }

测试输出:

3 11 2 9

16 1 7 10

12 8 13 15

4 6 5 14

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值