本帖最後由 b790113g 於 2012-2-25 11:59 編輯 3 N+ S, P! c/ G
" y& g: e ^1 q0 f% w/ O# k(1) 可判斷以逗號區隔之字串個數! B. |/ L/ @2 i) P. I
(2) 使用者輸入以逗號隔開不特定個數字串,程式將逗號兩旁之字擷取儲存,並分行顯示9 `1 {" ?) b' m$ O. [
(3) 若輸入空白字元,執行結果顯示警告訊息. ^* L& O6 `4 T1 u6 x [' K
2 k; Y5 g, f0 H( P# D; N" w
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] );
- }
- }
- }
複製代碼 |