split使用說明,()內加入用來分割token的符號,便會以此作分割
public static void main(String[] a)
{
String s = "12,34,56,78";
String[] z = s.split(",");
System.out.println(z.length);
for(String p:z){System.out.println(p);}
for(int i=0;i<z.length;i++){System.out.println(z[i]);}
}
split使用說明2,加入-1參數,表示輸入空自串也會被計入
public static void main(String[] a)
{
int sum=0; String s=""; String[] z= null; boolean fg=false;
do{
fg= false;
s = fun.Input("Enter a lot of number by coma to divid: ");
z = s.split(",",-1);/*-1 recive NULL*/
try{
sum=0;
for(String p : z){sum += Integer.parseInt(p);}
}catch(Exception e){fg = true;System.out.println("Reenter!!");}
}while(fg);
System.out.println("SUM= "+sum);
}
實驗:在java中使用多筆測資的語法思考 public static void main(String[] a) { int sum=0; String s=""; String[] z= null; boolean fg=false; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-->0){ fg= false; s=fun.Input("Enter a lot of number by coma to divid: "); z = s.split(","); do{ try{ sum=0; for(String p : z){sum += Integer.parseInt(p);} }catch(Exception e){fg = true;System.out.println("Reenter!!");} }while(fg); System.out.println("SUM= "+sum); } }
留言列表