本帖最後由 b790113g 於 2012-2-25 11:59 編輯 9 x2 \+ ~2 _# u" |
H% }8 j7 f- g8 F/ j
(1) 可判斷以逗號區隔之字串個數7 x. {) B' S8 M
(2) 使用者輸入以逗號隔開不特定個數字串,程式將逗號兩旁之字擷取儲存,並分行顯示
* \5 J, j8 ^+ b2 s# e; H" `(3) 若輸入空白字元,執行結果顯示警告訊息3 U9 J) V3 W1 @1 V7 I
1 w! g3 N9 h/ N8 B! ?import java.util.*;- import java.util.*; //引入才可使用split
- public class jva203{
- public static void main(String arg[]){
- if(arg.length!=1){
- System.out.println("參數借誤!字串中不得有空白或必須以逗號隔開!");
- return ;
- }
- //111,2222,3333 split() 必須引入 java.util.*
- String str[] = arg[0].split(",");
- System.out.println("逗號隔開的字串個數共有: "+str.length);
-
- for(int i=0;i<str.length;i++){
- System.out.println( (i+1)+"."+str[i] );
- }
- }
- }
複製代碼 |