03 - 条件,循环,方法

1. if else / switch

if else if else 
switch case  --> case穿透 

switch建议:判断固定值的时候用;
if建议:判断区间或范围的时候用;

--------------------

class Demo1_Sequence {                            //sequence 顺序
    public static void main(String[] args) {
        System.out.println("Hello World!11111");
        System.out.println("Hello World!3333");
        System.out.println("Hello World!22222");
        System.out.println("Hello World!44444");
    }
}

-------------------

import java.util.Scanner;
class Test1_If {
    public static void main(String[] args) {
        /*
        * A:练习1
        * 
                需求:键盘录入一个成绩,判断并输出成绩的等级。
                90-100 优
                80-89  良
                70-79  中
                60-69  及
                0-59   差
                
        * B:练习2
            * 需求:
                * 键盘录入x的值,计算出y的并输出。
                
                * x>=3    y = 2 * x + 1;
                * -1<x<3    y = 2 * x;
                * x<=-1    y = 2 * x - 1;
        */
        Scanner sc = new Scanner(System.in);

        //需求:键盘录入一个成绩,判断并输出成绩的等级。
        /*System.out.println("请输入学生成绩范围在1到100之间");
        int x = sc.nextInt();

        if (x >= 90 && x <= 100) {
            System.out.println("优");
        }else if (x >= 80 && x <= 89 ) {
            System.out.println("良");
        }else if (x >= 70 && x <= 79 ) {
            System.out.println("中");
        }else if (x >= 60 && x <= 69 ) {
            System.out.println("及");
        }else if (x >= 0 && x <= 59 ) {
            System.out.println("差");
        }else {
            System.out.println("成绩录入错误");
        }*/

        //需求: 键盘录入x的值,计算出y的并输出
        System.out.println("请输入一个整数:");
        int x = sc.nextInt();
        int y = 0;
        if (x >= 3) {
            y = 2 * x + 1;
        }else if (x > -1 && x < 3) {
            y = 2 * x;
        }else if (x <= -1) {
            y = 2 * x - 1;
        }

        System.out.println(y);
    }
}

----------------------

import java.util.Scanner;
class Test3_SwitchIf {
    public static void main(String[] args) {
        /*

        * 键盘录入月份,输出对应的季节
        一年有四季
        3,4,5春季
        6,7,8夏季
        9,10,11秋季
        12,1,2冬季
        */
        Scanner sc = new Scanner(System.in);    //创建键盘录入对象
        System.out.println("请输入月份");
        int month = sc.nextInt();                //将键盘录入的结果存储在month
        /*switch (month) {
        case 3:
        case 4:
        case 5:
            System.out.println(month + "月是春季");
        break;
        case 6:
        case 7:
        case 8:
            System.out.println(month + "月是夏季");
        break;
        case 9:
        case 10:
        case 11:
            System.out.println(month + "月是秋季");
        break;
        case 12:
        case 1:
        case 2:
            System.out.println(month + "月是冬季");
        break;
        default:
            System.out.println("对不起没有对应的季节");
        break;
        }*/

        //用if语句来完成月份对应季节
        if (month > 12 || month < 1) {
            System.out.println("对不起没有对应的季节");
        }else if (month >= 3 && month <= 5) {
            System.out.println(month + "月是春季");
        }else if (month >= 6 && month <= 8) {
            System.out.println(month + "月是夏季");
        }else if (month >= 9 && month <= 11) {
            System.out.println(month + "月是秋季");
        }else {
            System.out.println(month + "月是冬季");
        }
    }
}

2. for循环

/*
* A:案例演示
    * 需求:统计”水仙花数”共有多少个
    分析:
    1,需要有一个变量记录住水仙花数的个数
    2,获取到所有的3位数
    3,判断是否满足水仙花数
    4,如果满足条件,计数器就自增
*/
class Test4_FlowerCount {
    public static void main(String[] args) {
        int count = 0;

        for (int i = 100;i <= 999 ;i++ ) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 10 / 10;

            if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
                count ++;                                                    //满足条件就自增,计数器思想
            }
        }

        System.out.println(count);
    }
}

3. while循环, do...while,循环嵌套 

for循环与while循环的区别:
    如果你想在循环结束后,继续使用控制条件的那个变量,用while循环。
    否则用for循环。不知道用谁,就用for循环,因为变量及早的从内存中消失,可以提高内存的使用效率。


while语句的无限循环,for语句的无限循环

循环嵌套

-----------------------------
class Test1_While {
    public static void main(String[] args) {
        /*
        * A:求和思想
            * 求1-100之和
        * B:统计思想
            * 统计”水仙花数”共有多少个
        */
        
        //求1-100之和
        /*int sum = 0;
        int i = 1;
        while (i <= 100) {
            sum += i;                    //sum = sum + i;
            i++;                        //让变量i自增
        }

        System.out.println("sum = " + sum);*/

        //统计”水仙花数”共有多少个
        int count = 0;                    //计数器
        int i = 100;
        while (i <= 999) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;

            if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
                count ++;
            }

            i++;
        }

        System.out.println("count =" + count);

        //某屌丝为了追求女神,写了一段代码示爱,但是女神也会java,改动一下把屌丝拒绝
        int j = 1;
        while (j <= 10000) {
            System.out.println("I Love You!!!");
            j++;
        }
    }
}

-----------------------------

/*
* A:循环结构do...while语句的格式:
* 
        do {
            循环体语句;
        }while(判断条件语句);
        
        完整格式;
        初始化语句;
        do {
            循环体语句;
            控制条件语句;
        }while(判断条件语句);
* B:执行流程:
    * a:执行初始化语句
    * b:执行循环体语句;
    * c:执行控制条件语句
    * d:执行判断条件语句,看其返回值是true还是false
        * 如果是true,就继续执行
        * 如果是false,就结束循环
    * e:回到b继续。
* C:案例演示
    * 需求:请在控制台输出数据1-10
*/
class Demo1_DoWhile {
    public static void main(String[] args) {
        //while 和do while的区别
        /*int i = 11;
        do {
            System.out.println("i = " + i);
            i++;
        }
        while (i <= 10);
        
        System.out.println("---------------------");

        int j = 11;
        while (j <= 10) {
            System.out.println("j = " + j);
            j++;
        }*/

        /*for (int i = 1;i <= 10 ;i++ ) {
            System.out.println("i = " + i);
        }

        //System.out.println("i = " + i);            for语句执行后变量会被释放,不能再使用
        System.out.println("-------------------");
        int i = 1;
        while (i <= 10) {
            System.out.println("i = " + i);
            i++;
        }
        System.out.println("-------------------");
        System.out.println("i = " + i);                //while语句执行后,初始化变量还可以继续使用*/

        //while语句的无限循环
        /*while (true) {
            System.out.println("hello world");
        }*/

        //System.out.println("hello world");
        //for语句的无限循环
        for (; ; ) {
            System.out.println("hello world");
        }
    }
}

-------------------------------
/*
* A:案例演示
    * 需求:在控制台输出九九乘法表。

1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
...

*
**
***
*/
class Demo3_For99 {
    public static void main(String[] args) {
        /*for (int i = 1;i <= 9 ;i++ ) {                    //行数
            for (int j = 1;j <= i ;j++ ) {                //列数
                System.out.print(j + "*" + i + "=" + (i * j) + "\t" );
            }
            System.out.println();
        }*/

        //System.out.println("\"");                转义双引号
        System.out.println('\'');                //转义单引号
    }
}

4. break continue mark return 

break: 停止循环
return:返回方法
continue: 终止本次循环,继续下次循环。


/*
* A:break的使用场景
    * 只能在switch和循环中 
*/
class Demo1_Break {
    public static void main(String[] args) {
        for (int x = 1;x <= 10 ;x++ ) {
            if (x == 4) {
                break;                            //跳出循环
            }

            System.out.println("x = " + x);
        }
    }
}

------------------------

/*
* A:continue的使用场景
    * 只能在循环中 
*/
class Demo2_Continue {
    public static void main(String[] args) {
        for (int x = 1;x <= 10 ;x++ ) {
            if (x == 4) {
                continue;                            //终止本次循环,继续下次循环
            }

            System.out.println("x = " + x);
        }
    }
}

---------------------
class Demo3_Mark {                                        //mark 标记
    public static void main(String[] args) {
        /*outer: for (int i = 1;i <= 10 ;i++ ) {        //a就是标号,只要是合法的标识符即可
            System.out.println("i = " + i);
            inner: for (int j = 1;j <= 10 ;j++ ) {
                System.out.println("j = " + j);
                break outer;
            }
        }*/

        System.out.println("大家好");
        http://www.heima.com
        System.out.println("才是真的好");
        }
}

---------------------
class Demo4_Return {
    public static void main(String[] args) {
        for (int i = 1;i <= 10 ;i++ ) {
            if (i == 4) {                
                //break;                        //停止循环
                return;                            //返回的意思,用来返回方法
            }
        }

        System.out.println("循环结束了");
    }
}

5. 方法

方法重载:
    方法名相同,参数列表不同,与返回值类型无关。
    1. 参数个数不同
    2. 参数类型不同 (顺序不同)

/*
* A:案例演示
    * 需求:根据键盘录入的数据输出对应的乘法表
*/
import java.util.Scanner;
class Test2_Method {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);        //创建键盘录入对象
        System.out.println("请录入一个整数,范围在1-9之间");
        int num = sc.nextInt();                        //将键盘录入的整数存储在num中
        print99(num);
    }

    /*
    打印99乘法表
    1,返回值类型void
    2,参数列表,int a
    */

    public static void print99(int a) {
        for (int i = 1;i <= a ;i++ ) {                    //行数
            for (int j = 1;j <= i ;j++ ) {                //列数
                System.out.print(j + "*" + i + "=" + (i * j) + "\t" );
            }
            System.out.println();
        }
    }
}

-------------------------------------

/*
* A:案例演示
    * 需求:比较两个数据是否相等。
    * 参数类型分别为两个int类型,两个double类型,并在main方法中进行测试

*/
class Test3_Overload {
    public static void main(String[] args) {
        boolean b1 = isEquals(10,10);
        System.out.println(b1);

        boolean b2 = isEquals(10.5,10.5);
        System.out.println(b2);
    }

    /*
    比较两个数据是否相等
    1,返回值类型boolean
    2,参数列表int a,int b
    */

    public static boolean isEquals(int a,int b) {
        return a == b;
    }

    /*
    比较两个数据是否相等
    1,返回值类型boolean
    2,参数列表double a,double b
    */

    public static boolean isEquals(double a,double b) {
        return a == b;
    }
}
posted @ 2019-06-15 21:52  Alice的小屋  阅读(207)  评论(0编辑  收藏  举报