package kg.tom.chapter_five; //异常为循环而抛 public class Loop_42 { public static void main(String[] args) { int [][] tests = {{6,5,4,3,2,1},{1,2},{1,2,3},{1,2,3,4},{1}}; int successCount = 0; try { //不用使用异常控制循环,应该只为异常条件而使用异常 // int i =0; // while(true){ // if (thirdElementIsThree(tests[i++])) // successCount++; // } // for (int i = 0; i < tests.length; i ++){ // if(thirdElementIsThree(tests[i])) // successCount++; // // } //jdk 5.0以后 for(int[] test : tests){ if(thirdElementIsThree(test)) successCount++; } System.out.println(successCount); } catch (ArrayIndexOutOfBoundsException e) { // TODO: handle exception e.printStackTrace(); } } private static boolean thirdElementIsThree(int[] is) { //注意运算符重载问题!! // return is.length >=3 & is[2] == 3; // return is.length >=3 && is[2] == 3; } }
测试一下更新……………!!!