本帖最后由 凉宫蛋蛋 于 2012-7-22 03:42 编辑
- package Heima001_DumpAndDown100;
- import java.io.IOException;
- public class test {
- public static void main(String[] args) throws IOException {
- int counter = 0;
- while (true) {
- try {
- //1.1第一轮,count=0
- //1.2先判断 0 < 2?
- if (counter++ < 2) {
- //1.3上面判断成立,进入这里后,counter = 1
- System.out.println(counter);
- //1.4抛出异常,
- throw new Exception();
- }
- System.out.println("No Exception");
- //1.5
- } catch (Exception e) {
- //1.6
- System.err.println("MyNewException happened");
- //1.7
- } finally {
- //1.8
- System.err.println("finally is called");
- //1.9counter为1 1>2 ?不成立,回到while
- if (counter > 2) {
- System.out.println("循环了【" + counter + "】次");
- break;
- }
- }
- }
- }
- }
复制代码- package Heima001_DumpAndDown100;
- import java.io.IOException;
- public class test {
- public static void main(String[] args) throws IOException {
- int counter = 0;
- while (true) {
- try {
- //2.1第二轮,count=1
- //2.2先判断 1 < 2?
- if (counter++ < 2) {
- //2.3上面判断成立,进入这里后,counter = 2
- System.out.println(counter);
- //2.4抛出异常,
- throw new Exception();
- }
- System.out.println("No Exception");
- //2.5
- } catch (Exception e) {
- //2.6
- System.err.println("MyNewException happened");
- //2.7
- } finally {
- //2.8
- System.err.println("finally is called");
- //2.9counter为2 2>2 ?不成立,回到while
- if (counter > 2) {
- System.out.println("循环了【" + counter + "】次");
- break;
- }
- }
- }
- }
- }
复制代码- package Heima001_DumpAndDown100;
- import java.io.IOException;
- public class test {
- public static void main(String[] args) throws IOException {
- int counter = 0;
- while (true) {
- try {
- //3.1第三轮,count=2
- //3.2先判断 2 < 2? 不成立到catch
- if (counter++ < 2) {
-
- System.out.println(counter);
-
- throw new Exception();
- }
- System.out.println("No Exception");
-
- } catch (Exception e) {
- //3.3来到这里后counter为3
- System.err.println("MyNewException happened");
- //3.4
- } finally {
- //3.5
- System.err.println("finally is called");
- //3.6counter为3 3>2 ?成立
- if (counter > 2) {
- //3.7输出counter = 3
- System.out.println("循环了【" + counter + "】次");
- //3.8
- break;
- }
- }
- }
- //3.9跳出while
- }
- }
- counter++是先判断后自加
- ++counter就是先自加后判断
- 求加分
复制代码 |