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;
}