主題:一維數組
寫在一個維數組的大小先讀一個java程序,在雙類型的每個數組元素讀,顛倒了這個一維數組的順序。 不要創建另一個數組來保存結果。 提示:使用文本中的代碼交換兩個元素。
樣本輸出:
請輸入數組的大小:4
請輸入4雙號:1.0,2.0,3.0,4.0
相反的順序後:4.0,3.0,2.0, 1.0
我有2個問題與我的任務。想知道是否有人能幫我找到解決辦法。
- 我需要(雙[] myArray的),使用
掃描儀輸入從多個雙打單行填補數組的值。 - 我需要顛倒數組內的值的順序,而不使用 使用第二個數組。
這是我的代碼到目前爲止。
Scanner sc = new Scanner(System.in);
System.out.println("Please input the size of array: ");
int arraySize = sc.nextInt(); //Stores user input
double[] myArray = new double[arraySize]; //Create array[userInput]
System.out.print("Please input " + arraySize + " double numbers: ");//Collect user input
int j = 0;
while (sc.hasNext()) { //Another Failed Attempt to collect the input from a single line
if (sc.hasNextDouble()) {
myArray[j] = sc.nextDouble(); //Insert user input into array
j++;
}
}
Collections.reverse(Arrays.asList(myArray));//Doesn't work :(
System.out.println("After reverse order: ");
for (int i=0;i<arraySize;i++)
System.out.println(myArray[i]);
我的問題是,當雙打輸入由控制檯移動到下一行仍然期待用戶輸入,給定輸入是否給予ArrayIndexOutOfBounds
被拋出
什麼是你的代碼錯誤的完全反轉?它不會編譯/拋出異常/返回錯誤的結果嗎? – Pshemo 2014-10-18 15:51:58
我沒有看到你的問題描述/錯誤 – Dima 2014-10-18 15:52:30
使用堆棧! ;-) – jahroy 2014-10-18 15:55:03