如果有人可以回答,那将会非常感激。
这是我到目前为止所做的:
public class Array {
public static void main (String[] args) {
int array[] ={23, 24, 25, 26, 27, 28, 29};
int array2[] = new int[array.length];
int count = 0;
for (int i= 0;i < array.length;i ++)
{
if (array[i] % 2 == 0 && array[i] > 25) {
array2[count] = array[i];
count ++;
}
}/* Here is how I print it and it gives me the answer and the remaining
elements as O s.*/
for(int i=0; i<array2.length; i++){
System.out.println(array[i]);
}
}
}
对不起的错误,现在纠正了。
答案 0 :(得分:1)
首先,您必须声明i
是什么。您可以通过更改
for (i= 0;i < array.length;i ++)
进入
for (int i= 0;i < array.length;i ++)
或在循环之前声明i
int i;
来到问题 "Create a new Array taking only elements that are even and bigger than 25"
当你声明并初始化一个与第一个数组长度相同的数组时,即使你没有初始化剩余的元素,它们的默认值也是零;为了解决这个问题,你可以创建一个新数组并将所有非零元素传递给它。
int[] arr3 = new int[count];
for(int i=0; i<array2.length; i++) {
if(array2[i] != 0) arr3[i] = array2[i];
}