zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    oracle数据库同步
    软件设计过程中常用的几种图(一)
    面向对象建模为基础的开发模式
    常用正则表达式
    js 创建一个浮动div
    DATALIST 绑定数据分页
    ASP.NET 3.5 Extensions CTP Preview Released
    重构是一种意识流
    重构随笔: 封装集合(Encapsulate Collection)
    乱笔.................table控件,带一列合并
  • 原文地址:https://www.cnblogs.com/hahajava/p/10087674.html
Copyright © 2011-2022 走看看