【发布时间】:2015-10-19 21:06:45
【问题描述】:
这是一种生成随机数的算法,直到某个数字的每个数字都大于前一个数字。 (例如:1234、1589 或 6789 而不是 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 总是为真,所以你需要一个中断来退出内部的 while,然后去 do while 并打印 System. out.print(entierAleatoire + " ");