1. 程式人生 > >for循環相關

for循環相關

ole 但是 print 輸出結果 sco 相關 current 一次 for循環

publicclass Test2 {
    staticboolean foo(char c) {
       System.out.print(c);
       returntrue;
    }
    publicstaticvoid main(String[] argv) {
       int i = 0;
       //for(65;88&&(i<2);67)
       for (foo(‘A‘); foo(‘B‘) && (i < 2); foo(‘C‘)) {
           i++;
           foo(
‘D‘); } } } /* What is the result? A. ABDCBDCB B. ABCDABCD C. Compilation fails. D. An exception is thrown at runtime. //輸出結果是:ABDCBDCB 分析:FOR循環裏面講究的條件要為真,與你的判斷式是什麽沒有關系 就像這裏,雖然是打印的字母,但是卻不是false,所以可以執行 第一次進行循環: foo(‘A‘)打印字母A,(註:這裏不是false條件就默認為true條件) foo(‘B‘)打印字母B,i=0,比較(i < 2),條件為true,進行循環體,foo(‘D‘)打印D foo(‘C‘)打印字母C 第二次循環: foo(‘B‘)打印B,i=1,比較(i < 2)為true,進行循環體,foo(‘D‘)打印D foo(‘C‘)打印字母C 第三次循環: foo(‘B‘)打印字母B,i=2,比較(i < 2)為false,退出循環,得結果
*/

    public static void main(String[] args) throws ParseException {


        StringBuffer sb2 = new StringBuffer();
        String str="da壹f零tghr";
        String containType="";
        int co=0;
        long t1 = System.currentTimeMillis();
        for (; ; ) {
            co++;
            containType 
= RegeUtils.isContainType("零|壹|貳|叁|肆|伍|陸|柒|捌|玖|拾|佰|仟|萬|億|角|分|元|圓|整", str); if (StringUtils.isBlank(containType)) { break; } if ("整".equals(containType)) { sb2.append(containType); break; } str = str.substring(str.indexOf(containType) + 1, str.length()); sb2.append(containType); } long t2 = System.currentTimeMillis(); System.out.println("-4.9-"+ String.format(": %s", (t2-t1))); System.out.println("-5-"+ String.format(": %d,%s", co, sb2.toString()) ); // 3,壹零 } public static String isContainType(String type, String str) { Pattern p = Pattern.compile(type); Matcher m = p.matcher(str); if (m.find()) { return m.group(); } return null; }

for循環相關