[英]Fill array with odd numbers
I have to fill an array with a range (here n
) of odd numbers: 1,3,5,7,9... But I always have a 0 between each odd numbers and I don't understand why.我必须用奇数范围(此处为
n
)填充数组:1,3,5,7,9...但我总是在每个奇数之间有一个 0,我不明白为什么。
Note: The code under the capital letters in comments were given by our professor...注:评论中大写字母下的代码是我们教授给的...
Here is the code:这是代码:
public class F5B1 {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
// Creation of Array : DON'T CHANGE THIS PART OF THE PROGRAM.
System.out.print("Entrez n : ");
int n = scanner.nextInt();
int[] t = new int[n];
int i;
// fill the array : PART OF THE PROGRAM TO FULFILL
// INSTRUCTIONS :
// ADD NO ADDITIONAL VARIABLES
// DON'T USE ANY OF THE MATH METHODS
// DON'T ADD ANY METHODS
// I wrote this piece of code
for (i = 0; i < t.length; i++) {
if (i % 2 != 0) {
t[i] += i;
}
}
// display of the array : DON'T CHANGE THIS PART OF THE PROGRAM
System.out.println("Those are the odd numbers : ");
for (i = 0; i < t.length; i++) {
System.out.println(t[i]);
}
}
}
The output:输出:
Enter n : 10
Those are the odd numbers :
0
1
0
3
0
5
0
7
0
9
You are getting 0
for each even index because the value at that index is never set.每个偶数索引都为
0
,因为从未设置该索引处的值。
This line:这一行:
int[] t = new int[n];
declares an array of int of length n
where each element is initialized to 0. Now consider your code:声明一个长度为
n
的 int 数组,其中每个元素都初始化为 0。现在考虑您的代码:
for (i = 0; i < t.length; i++) {
if (i % 2 != 0) {
t[i] += i;
}
}
You are saying: when the index of my array is odd, let's set it to this index, otherwise, do nothing (and keep 0).你是说:当我的数组的索引是奇数时,让我们将它设置为这个索引,否则,什么都不做(并保持 0)。 This explains the result you are getting.
这解释了你得到的结果。
What you want to do is not to check whether the index of the array is odd: you want to set an odd value to each index of the array.您要做的不是检查数组的索引是否为奇数:您想为数组的每个索引设置一个奇数。 Consider this code instead:
请考虑以下代码:
for (i = 0; i < t.length; i++) {
t[i] = 2 * i + 1;
}
For each index, this sets the value of the array to an odd number ( 2n+1
is always odd).对于每个索引,这会将数组的值设置为奇数(
2n+1
总是奇数)。
(Note that I wrote =
instead of +=
: it gives the intent better and does not rely on the fact that the array was initialized with 0) (请注意,我写的是
=
而不是+=
:它提供了更好的意图,并且不依赖于数组初始化为 0 的事实)
With Java 8, IntStream it is as simple as:在 Java 8 中, IntStream就像这样简单:
IntStream.range(0, n).filter(element -> element % 2 != 0)
.forEach(System.out::println);
Usage:用法:
public class F5B1 {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
System.out.print("Entrez n : ");
int n = scanner.nextInt();
IntStream.range(0, n).filter(element -> element % 2 != 0)
.forEach(System.out::println);
}
}
Try this instead :试试这个:
int odd = 1;
for (int i = 0; i < t.length; i++) {
t[i] = odd;
odd += 2;
}
The problem is that this :问题在于:
int[] t = new int[n];
will create an array filled with zeros.将创建一个填充零的数组。 In your for loop you are setting the odd numbers so the others are left at zero.
在您的 for 循环中,您正在设置奇数,以便其他人保持为零。
for (i = 0; i < t.length; i++) {if (i % 2 != 0) { t[i/2] = i; for (i = 0; i < t.length; i++) {if (i % 2 != 0) { t[i/2] = i; } }
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.