Re: [問題] String 取特定的字

看板java作者 (Alien)時間16年前 (2009/03/26 15:41), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/5 (看更多)
※ 引述《windknife18 (windknife18)》之銘言: : String str = "101110"; : int count = 0; : for(int i=0 ; i<str.length() ; i++){ : if(str.charAt(i) == '1') : count = count + 1; : } : System.out.println("Your number include " + count+ " 1."); Java5 或以上的話 String str = "10110"; int count = 0; for (char c : str.toCharArray()) { if (c == '1') { ++count; } else if (c != '0') { // System.out.println("invalid input"); } } 順便也做檢查了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.155.236.82 ※ 編輯: adrianshum 來自: 202.155.236.82 (03/26 18:32)
文章代碼(AID): #19op6ujJ (java)
文章代碼(AID): #19op6ujJ (java)