2011-12-07 194 views
0

對不起,我的問題很難理解。因此,在一個陣列,如 的String [] ARR = {「ABS‘高清’,‘GHI’}; 我想知道如何能打印出此 一個 ê 我如何打印出字符串索引字符串中字符串的字符

這是我的代碼我試圖寫,但未能

public static void printCharsAt(String[] arr) { 
    System.out.println("how many inputs do you want in your array"); 
    String array[] = {"hello", "Ap", "CS"};   
    System.out.println("What term of the array do you want, do you wan the first, 2nd, or third"); 
    tnt char = kb.nextInt(); 
    System.out.println(array[char]); 
} 

回答

3

沒有任何邊界檢查:

for (int i=0; i<arr.length; i++) 
    System.out.println(arr[i].charAt(i)); 
2

您必須遍歷數組中的每個字符串。您可以使用Java中的for each循環或傳統的for循環。獲得字符串後,可以使用String類的charAt()方法獲取特定位置的字符。不要忘記length方法,它可以派上用場。沒有代碼,因爲它是作業:)