1

I have a question as I'm trying to create a generator in java that generators random letters and this is the code I have so far and it works. But I have to have the 5th place of my generated letters be a number 0-9 and I am completely lost on how to do so.

My question is how do I set my generator to pick Only the 5th spot from my generated letters as a number only.

Example my generator will produce something such as :PMWK S DELJG but I don't know how to make the bold character only a number and the rest still letters.

        import java.util.Random;

    public class RandomGen {
       public static void main(String[] args) {
       final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       final int N = alphabet.length();
       Random r = new Random();
       for (int i = 0; i < 10; i++) {
           System.out.print(alphabet.charAt(r.nextInt(N)));
    }           
  }
 }
4

1 回答 1

1

Replace the inside of the loop with this code:

if (i==4) {
      System.out.print(r.nextInt(10);
} else {
      System.out.print(alphabet.charAt(r.nextInt(N)));
}
于 2013-11-03T23:36:06.010 回答