我必須用奇數的範圍(這裏是n
)填充一個數組:1,3,5,7,9 ......但是每個奇數和我之間總是有一個0不明白爲什麼。用奇數填充數組
注:評論信件是由我們的教授給出的資本項目下的代碼...
下面是代碼:
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]);
}
}
}
輸出:
Enter n : 10
Those are the odd numbers :
0
1
0
3
0
5
0
7
0
9
您可以張貼輸出嗎? – dsharew
是的,我當然會這樣做 – algorithmic
你的教授通過讓你在for循環之外聲明'i'給你不好的建議 –