Java学习:第四章控制执行流程

do-while:

public class DoWhile {
    float flag = 1;
    void print() {
        flag = flag + 0.1f;
        System.out.println(flag);
    }


    public static void main(String[] args)
    {
        DoWhile obj = new DoWhile();
        float value = obj.flag;
        do
        {
            System.out.println(value);
            value +=0.1f;
        }
        while (value<=2.0f); // 布尔表达式
        {
            System.out.println(false);
            obj.print();
        }
    }
}

Foreach:

//★     Java SE5引入一种新的更加简洁的语法
//来访问数组和容器,即Foreach语法
//◆声明数组的语法:
//        (1)  数组元素类型[ ]  数组引用;
//        (2)  数组元素类型  数组引用[ ];//数组名也是引用
//创建数组空间的语法:
//数组引用 = new  数组元素类型[数组元素个数];
//float[ ] f = new float[5];  //数组也是对象
import java.util.*;

public class FOREACH {
    public static void main(String[] args) {
        Random rand = new Random(47);
        float[] f = new float[5];
        //  传统:利用循环控制变量控制数组下标
        for (int i = 0; i < f.length; i++) {
            f[i] = rand.nextFloat();//[0,1)
            //  调用随机数对象中的方法以产生随机数
        }
        //  Foreach语法:将每一个f的元素赋值给x
        for (float x : f) {
            System.out.println(x);
        }
        for (char c : "An African".toCharArray()) {
            System.out.println(c + "  ");
        }

        //break用于强行退出当前循环。而continue则停止执行当前的循环,然后从循环起始处,开始下一次的循环。
        for(int i = 0; i < 100; i++)
        {
            if(i == 74) break;
            if(i % 9 != 0) continue;
            System.out.println(i);
        }

    }
}

If-Else:

public class IfElse {
    static int result = 0;
    static void test(int t1, int t2) {
        if(t1 > t2)
            result = -1;
        else if(t1 < t2)
            result = +1;
        else
            result = 0;
    }
    public static void main(String[ ] args) {
        test(1, 5);
        System.out.println(result);
    }
}

Label:

public class label {
    private static void prt(String s) {
        System.out.println(s);
    }
    public static void main(String[] args){
        int i = 0;
        outer:        // 标签和循环之间不应有任何语句
        for( ; true ;)
        {
            prt("sigal to outer");
            inner:      // 标签和循环之间不应有任何语句
            for( ; i < 10; i++)
            {
                prt("i = " + i);
                if(i == 2) { prt("continue");   continue; }
                if(i == 3) { prt("break"); i++;   break; }

                if(i == 7) { prt("continue outer"); i++; continue outer; }
                if(i == 8) { prt("break outer"); break outer; }
                for(int k = 0; k < 5; k++)
                    if(k == 3) {  prt("continue inner"); continue inner; }
            }
        }
    }
}

Switch:

public class Swith {

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            char c = (char) (Math.random() * 26 + 'a');
            System.out.print(c + ": ");
            switch (c) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    System.out.println("vowel");
                    break;
                case 'y':
                case 'w':
                    System.out.println("Sometimes a vowel");
                    break;
                default:
                    System.out.println("consonant");
            }

        }
    }
}

While:

public class While {
    static boolean condition( ){
        boolean result = Math.random( )< 0.99;
        System.out.println(result);
        return  result;
    }
    public static void main(String[ ] args) {
        while(condition( )) ;
    }
}

迭代(for)

public class 迭代 {
    public static void main(String[] args) {
        for (char c = 0; c < 128; c++) {
            if (Character.isLowerCase(c)) {
                System.out.println("value: " + (int) c);
                System.out.println("Character: " + c);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值