Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1708391
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Java

2008-08-03 23:58:18

1.   流程控制

1.1. 条件判断

1.1.1. 代码

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package javatutorials;

 

/**

 *

 * @author wanpor

 */

public class Condition {

    static int left = 4;

    static int right = 8;

    public static void main(String[] args){

 

        ifelse();

        switchdefault();

    }

    static void ifelse(){

        //if语句

        if(left > right){

            System.out.println("left");

        }

       

        //if...else...语句

        if(left > right){

            System.out.println("left");

        }else if (left < right){

            System.out.println("rigth");

        }else if (left == right){

            System.out.println("left & right");

        }       

    }

    //switch语句

    static void switchdefault(){

        switch(left - right){

            case 0:System.out.println("left & right");break;

            case 1:System.out.println("left");break;

            case 2:System.out.println("right");break;

            default:System.out.println("default");

        }

    }

}

1.1.2. 说明

1.       单条件判断if(a > b) System.out.println(a);

2.       双条件判断if(a > b) System.out.println(a); else System.out.println(b);

3.       多条件判断,else分支中使用嵌套;或使用switch控制

1.2. 循环控制

1.2.1. 代码

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package javatutorials;

 

/**

 *

 * @author wanpor

 */

public class Loop {

 

    static int left = 4;

    static int right = 8;

    public static void main(String[] args){

        dowhile();

        dofor();

    }

    //while/do...while语句

    static void dowhile(){

        do{

            System.out.println(left--);

        }while(left > 0);

        while(left < 4){

            System.out.println(left++);

        }

    }

    //for语句

    static void dofor(){

        for(int i = 0; i < left;i++){

            System.out.println(left - i);

        }

        for(int i = left;i > 0; i--){

            System.out.println(left - i);

        }

    }

}

1.2.2. 说明

do…whle先执行再进行判断,程序至少执行一次;

while先判断再执行,循环体可能不执行;

for执行特定次数的重复操作;

1.3. 返回控制

1.3.1. 代码    

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package javatutorials;

 

/**

 *

 * @author wanpor

 */

public class Return {

    static int left = 4;

    static int right = 8;

    public static void main(String[] args){

       

    }

    //只打印小于2的值

    static void breakfor(){

        for(int i = 0; i < left;i++){

            if(i == 2)

                break;

            System.out.println(i);

        }

    }

    //只打印小于2的值

    static void returnfor(){

        for(int i = 0; i < left;i++){

            if(i == 2)

                return;

            System.out.println(i);

        }

    }

    //大于不等于2的值

    static void continuefor(){

        for(int i = 0; i < left;i++){

            if(i == 2)

                continue;

            System.out.println(i);

        }

    }

   

 

}

1.3.2. 说明

break:中断整个循环

continue:中断一下循环

return:直接从函数返回

文件:javatutorials.zip
大小:1KB
下载:下载
阅读(1966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~