我需要生成一個介於0和9999之間的隨機數,然後我需要將這些數字全部四個字符長 - 因此,如果1238生成的很好,但如果96產生需要顯示0096,或3將是0003如何製作一個長度爲4個字符的隨機數 - Java
這是到目前爲止我的代碼...
public static void main(String[] args) {
Random randomGenerator = new Random();
for (int i = 0; i < 5; i = i++) {
int randomInteger = randomGenerator.nextInt(9999);
System.out.println(randomInteger);
}
}
}
'的System.out。 printf(「%04d」,randomInteger);' – lurker
謝謝!我也需要這樣做,所以它不使用1111,2222,3333,4444等9999如何做到這一點? – Demi
你的問題根本就沒有提到。您能否更新您的問題(使用[編輯](http://stackoverflow.com/posts/33827593/edit)鏈接)以清楚地解釋您的所有要求?例如,你想要什麼和什麼之間的隨機數字?你想避免什麼樣的數字? – lurker