2015-07-19 68 views
0

我試圖得到一堆數字作爲輸入,並輸出它作爲第一個未排序的數組,然後排序。如何將輸入值存儲在for循環內的數組中?現在,它只存儲最後輸入的值。如何將值存儲在for循環中的數組中?

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 
Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 
strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 
for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    for (int j = 0; j == 0; j++) { 
     int[] intArray = { 
      intValue 
     }; 
     System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 

回答

4

聲明並在循環開始前,初始化數組:

intNumberOfValues = Integer.parseInt(strNumberOfValues); 
int[] intArray = new int[intNumOfValues]; 
for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intArray[i] = Integer.parseInt(strValue); 
} 
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
. . . 

請注意,你的內循環是沒用的。它只執行一次,循環索引j不用於循環。它所做的只是更多地限制intArray的聲明範圍,因此即使打印數組值的行也未定義該符號。順便說一下,該打印語句不應該執行,直到所有輸入值已被獲取,這就是爲什麼我將它移到我的答案中的外環之後。 (還請注意,變量intValue不再需要在這裏,可以從程序中刪除,除非在別處使用。)

作爲一種風格,我還建議您避免使用變量類型作爲前綴爲你的變量名稱。

0

您必須首先聲明數組,然後根據索引分配值。

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 

Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 

strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 

int [] intArray = new int[intNumberOfValues]; 

for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    intArray[i] = intValue; 
} 
    System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
+0

究竟爲什麼你重新職位,給予近三分鐘前一個答案? – Turing85

+0

:-)我在5分鐘之前開始回答這個問題。但格式化需要更多時間。 – ganeshvjy

0

在您的inner for循環中,您每次都重新聲明數組,所以只有最後一個值纔會被保存。你必須與大小事先聲明數組,然後輸入用戶在一個for循環填充它指數指數:

String strNumberOfValues, strValue; 
int intNumberOfValues, intValue; 
Scanner input = new Scanner(System. in); 
System.out.println("Please enter the number of values you would like to enter"); 
strNumberOfValues = input.nextLine(); 
intNumberOfValues = Integer.parseInt(strNumberOfValues); 
int[] intArray = new int[intNumberOfValues]; 

for (int i = 0; i < intNumberOfValues; i++) { 
    System.out.println("Please enter value for index value of " + i); 
    strValue = input.nextLine(); 
    intValue = Integer.parseInt(strValue); 
    intArray[i] = intValue; 
} 
System.out.println("Here is the unsorted list: \n" + Arrays.toString(intArray)); 
+0

請不要將數組稱爲「列表」。他們根本不同。 – Turing85

+0

@ Turing85我剛剛在原始問題中複製了該打印語句。輸出可能不適用於技術用戶。我認爲單詞「list」被用作一個正常的單詞,而不是對編程結構的引用。 – Zarwan

+0

這是不正確的。一個'List'是一個動態數據結構。數組不是。任何使用某種命令式編程語言的人都應該知道差別(例如OP)。如果你不想編寫「數組」(對於非技術性的最終用戶),你可以通過寫一些類似''的東西來避免這種情況。「這裏是你輸入的元素:''。 – Turing85

相關問題