Think in java 答案_Chapter 3_Exercise 5

来源:互联网 发布:用ipad怎么看淘宝直播 编辑:程序博客网 时间:2024/05/01 19:04

阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

/****************** Exercise 5 ******************
* Modify Exercise 4 so that the program exits by
* using the break keyword at value 47. Try using
* return instead.
***********************************************/

public class E05_Break47 {
  public static void main(String[] args) {
    for(int i = 1; i <= 100; i++) {
      System.out.println(i);
      // if(i == 47) break;
      if(i == 47) return;
    }
  }

 //+M java E05_Break47

**The above example includes a commented line of code to use break, as well. The break will exit the for loop, which puts it at the end of main( ), whereas return exits directly from the current method, which happens to be main( ).

原创粉丝点击