這是一種生成隨機數的算法,直到每個數字大於前一個數字的數字爲止。 (例如:1234,1589或6789 NOT 1233或1334)然後,它將所有生成的數字打印到控制檯上。我無法在顯示數字之前添加分號;命令在if()中,但是爲什麼?爲什麼我必須打破;在這裏循環?
public static void numeroNeuf() {
boolean croissant = false;
do {
int entierAleatoire = rnd.nextInt(10000)+1;
System.out.print(entierAleatoire + " ");
while (entierAleatoire > 0) {
int chiffre1 = entierAleatoire % 10;
entierAleatoire /= 10;
int chiffre2 = entierAleatoire % 10;
if (chiffre2 > chiffre1 || chiffre2 == chiffre1) {
croissant = false;
break;
} else {
croissant = true;
}
}
} while (croissant == false);
}
輸出:
1742 8912 1104 7216 7473 3276 3267 8780 7583 2143 8285 7555 6812 1893 2188 5351 5427 780 9211 2618 1605 3719 511 7671 5839 735 654 8075 7989 7702 891 4850 2891 3529 1420 642 2723 7217 1629 9742 9408 3910 2301 6936 3865 193 3221 6343 8505 8268 4489 3872 6643 5017 1367
因爲如果你沒有休息,entierAleatoire> 0的條件總是成立的,所以你需要休息一會兒,然後去做,並打印System.out.print(entierAleatoire +「」); – Bak