中软面试(一):java写一个随机生成四位数的程序 每位数字不重复

来源:互联网 发布:三六五网络面试 编辑:程序博客网 时间:2024/05/03 19:22

import java.util.Random;
public class Test {
        public static void main(String[] args) {
                 Random r=new Random();
                 int tag[]={0,0,0,0,0,0,0,0,0,0};
                 String four="";
                 int temp=0;
                 while(four.length()!=4){
                         temp=r.nextInt(10);//随机获取0~9的数字
                         if(tag[temp]==0){
                               four+=temp;
                              tag[temp]=1;
                         }
                 }
                System.out.println(four);
         }
}