我的代碼是:我的數組是如何保存我輸入的最後一個數字的?
int total = 10;
int[] myArray = new int [total];
System.out.println("Enter numbers. To stop, enter 0.")
int numbers = input.nextInt();
while (numbers != 0) {
for (int i = 0; i < total; i ++)
myArray[i] = numbers;
numbers = input.nextInt();
}
for (int i = 0; i < myArray.length; i++)
System.out.print(myArray[i] + ", ") //This line only prints the last number I enter 10 times.
我希望能夠用我輸入的數字打印整個數組。例如:
我進入:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0
但結果我得到的是:10, 10, 10, 10, 10, 10, 10, 10, 10, 10
編輯:我不知道爲什麼,我的問題已經被標記重複?我試着在這個網站的任何地方尋找類似的問題,但沒有找到,所以我就問這個問題。這不是這個網站的目的嗎?
編輯2:好。我明白了。我會把我的問題帶到其他更有用的網站。感謝您的「服務」堆棧交換。
你認爲你的for循環做什麼? –
從你的代碼,我的理解,你可能會嘗試輸入,直到用戶輸入'0',或者你的數組已滿?我對嗎? –
+穆罕默德是的。這就是我想要的。 – 5120bee