你好我正试图让我的数组“数字”打印出所有偶数数字由于某种原因我不能让它从我之前的代码中调用数组,以便可以在下面使用该数组。 / p>
import java.util.Random;
public class Main {
public static void main(String [] args){
int numbers[] = new int [10];
for (int x = 0; x < numbers.length; x++){
Random rand = new Random();
numbers[x] = rand.nextInt(100)+1;
System.out.print("" + numbers[x] + " ");
}
System.out.println("");
System.out.print("The values at even indexes are: " + numbers[0] + " " + numbers[2] + " " + numbers[4] + " " + numbers[6] + " " + numbers[8]);
System.out.println("");
if(numbers[x] % 2 == 0){
System.out.print(numbers[x]);
}
}
}
答案 0 :(得分:1)
您将if(numbers[x] % 2 == 0)
置于for循环外的问题,这样您就无法通过索引x
迭代数组:
for (int x = 0; x < numbers.length; x++){
if(numbers[x] % 2 == 0){
System.out.print(numbers[x]);
}
}