【发布时间】:2019-09-19 01:34:59
【问题描述】:
我正在尝试将随机数组值与下一个值进行比较,但 java 无法识别 if 语句中的比较。最终,我将需要比较数组中的每个值,以便在每个重复值周围放置括号。我怎样才能解决这个问题?谢谢。
Random rand = new Random();
int dice[] = new int[20];
for (int i = 0; i < dice.length; i++) {
dice[i] = rand.nextInt(6) + 1; //assigns values to array
}
System.out.print(Arrays.toString(dice)); //prints whole array
System.out.println(" ");
if (dice[0] == dice[0] + 1) { //right here, nothing is printed
System.out.println("It doesn't work.");
}
【问题讨论】:
-
因为你用随机数填充数组
-
dice[0] == dice[0] + 1永远不会是真的——想想吧 -
@ScaryWombat 哦,天哪,我太愚蠢了。谢谢。