2015-10-02 136 views
0
Scanner input= new Scanner(System.in); 
int x=0; 
String [] storage=new String[3]; 
while (x< 3){ 
    storage[x]= input.nextLine(); 
    x++; 
} 

這是我的代碼到目前爲止。它能夠提示用戶輸入指定數量的輸入。我試圖檢查輸入是否真的存儲到存儲陣列。打印語句的當前位置不起作用,因爲數組「無法訪問」。將用戶輸入存儲在字符串數組中?

回答

0

在Java打印陣列

String[] strArray = new String[] {"Reema", "Seema", "Neha"}; 

Arrays.asList(strArray).stream().forEach(s -> System.out.println(s)); 

for (int i = 0; i < intArray.length; i++) { 
    System.out.print(intArray[i] + ", "); 
} 

String line1 = scan.nextLine(); // Read 1st line 
String[] numbers1 = line1.split(" "); // Split based on space 
for(int i=0;i<numbers1.length;i++){ 
    array[i] = Integer.parseInt(numbers1[i]); 
} 

String line2 = scan.nextLine(); // Read 2nd line 
String[] numbers2 = line2.split(" "); // Split based on space 
for(int i=0;i<numbers2.length;i++){ 
    array2[i] = Integer.parseInt(numbers2[i]); 
} 

價:How to store input values in an array by using Scanner in Java

相關問題