//根據不同的難度產生隨機字母和數字
for(int i=0; readomNumStart.length()<4; i++) {
if(difficult == 1) {
//產生隨機的0-9的數字
a = String.valueOf((int)(Math.random() * 10)) ;
}
if(difficult == 2) {
//在0-9和a,b,c,d中隨機產生。
String[] readomWord = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d"};
int readomWordIndex = (int)(Math.random() * 13);
a = readomWord[readomWordIndex];
}
if(difficult == 3) {
//產生隨機的字母
char readomLetter = (char)(Math.random ()*26+'a');
a = String.valueOf(readomLetter) ;
}
if(difficult == 4) {
//產生隨機的數字和字母的組合
String[] readomHard = new String[20];
int readomWordIndex = (int)(Math.random() * 19);
for(int j=0; j<20; j++) {
int readomWordNum = (int)(Math.random() * 10);
char readomLetter = (char)(Math.random ()*26+'a');
if(readomWordNum % 2 == 0) {
readomHard[j] = readomWordNum + "";
}else{
readomHard[j] = String.valueOf(readomLetter);
}
}
a = readomHard[readomWordIndex];
}
//
這樣寫代碼比較短。我也參考了許多人的代碼大部分都很復雜。我不喜歡寫那麼多代碼,所以就這樣寫了。還不能知道這樣寫效率有沒有問題,在我本機上運行沒有什麼感覺。