-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerificationGenerator.java
More file actions
36 lines (33 loc) · 1.19 KB
/
VerificationGenerator.java
File metadata and controls
36 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Cases;
import java.util.Random;
public class VerificationGenerator {
/*analysing:
* 1. does this method receive data? receive an integer to control the length of verification code
* 2. does this method return data? return generated verification code
* 3. logic within the method: using for loop to generate random character for every digit
* then using a string variable to connect each character, return this variable*/
public static void main(String[] args) {
System.out.println(createCode(5));
}
public static String createCode(int n){
Random r = new Random();
String code = "";
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type){
case 0:
code += r.nextInt(10);
break;
case 1:
char ch1 =(char)(r.nextInt(26) + 65);
code += ch1;
break;
case 2:
char ch2 = (char)(r.nextInt(26) + 97);
code += ch2;
break;
}
}
return code;
}
}