try:int genInt(int n){
    int result = 0;
    byte[] byts = new byte[n];
    for(int i = 0; i < n; i++){
      byts[i]  = (byte)(Math.random() * 10);
      if(i == 0){
        while(byts[0] == 0)
          byts[0] = (byte)(Math.random() * 10);
      }
      for(int j = 0; j < i; j++)
         if(byts[i] == byts[j]){
            byts[i] = (byte)(Math.random() * 10);
            j = -1;
            continue;
         }
      result = result * 10 + byts[i];    
   } 
   return result;
}

解决方案 »

  1.   

    Random rand = new Random();
    int ran = Math.abs(rand.nextInt(9000)+1000);
    String str = Integer.toString(ran);
    while(str.charAt(0)==str.charAt(1)||str.charAt(0)==str.charAt(2)
       ||str.charAt(0)==str.charAt(3)||str.charAt(1)==str.charAt(2)
       ||str.charAt(1)==str.charAt(3)  ||str.charAt(2)==str.charAt(3) )
    ran = Math.abs(rand.nextInt(9000)+1000);为什么不行呢?
      

  2.   

    while(str.charAt(0)==str.charAt(1)||str.charAt(0)==str.charAt(2)
       ||str.charAt(0)==str.charAt(3)||str.charAt(1)==str.charAt(2)
       ||str.charAt(1)==str.charAt(3)  ||str.charAt(2)==str.charAt(3) ){
          ran = Math.abs(rand.nextInt(9000)+1000);
          str = Integer.toString(ran);//U have to reassign variable str 
       }