Home > Back-end >  Java to generate random letters and Numbers
Java to generate random letters and Numbers

Time:10-12

How to generate similar a1234 such number, the first is a lowercase letter, four digits after it

CodePudding user response:

 public class NumberTest {

Public static void main (String [] args) throws the Exception {
The String result=Character. ToString ((char) generateNumberInRange (97, 122));
For (int I=1; I & lt;=4; I++) {
The result +=generateNumberInRange (0, 9);
}

System. The out. Println (result);
}

Private static int generateNumberInRange (int min, int Max) {
If (min & gt;=Max) {
Throw new IllegalArgumentException (" invalid range ");
}

The Random r=new Random ();
Return r.n extInt ((Max - min) + 1) + min;
}
}
  • Related