首先,遺憾的是代碼有點雜亂。它格式不是很好。我的任務是隨機生成由大寫和小寫字母組成的密碼,長度由用戶指定。到目前爲止,我的代碼還沒有完成,但那是因爲我遇到了一個問題。(Java)條件語句
我的密碼將顯示小寫和大寫字母,但不夠長。
例如,如果用戶想要一個長度爲14的密碼,我只會得到一個小於這個密碼的密碼。它永遠不會是它應該是的長度。
import java.util.Scanner;
import java.util.Random;
public class Password{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int passwordLength = 0;
Random randNum = new Random();
int randNumAscii = 0;
String generatedPassword = "";
System.out.print("Password Length (1-14): ");
passwordLength = in.nextInt();
for(int count = 0; count < passwordLength; count++){
randNumAscii = randNum.nextInt(123);
if(randNumAscii >= 65 && randNumAscii <= 90 || randNumAscii >= 97 && randNumAscii <= 122)
generatedPassword += (char)randNumAscii;
else
randNumAscii = randNum.nextInt(123);
}
System.out.println(generatedPassword);
}
}
任何人都可以提供幫助嗎? – user1713336
匆匆一瞥,我不確定你的問題是什麼。 – Makoto
因此,當用戶選擇數字2並假設長度爲14時,生成的密碼應該由長度爲14個字符的小寫字母和大寫字母組成。我的問題是它最終不到14個字符。 – user1713336