Java顺序结构、选择结构、循环结构、方法的简单定义

学习目标:

  • 观看Java视频

学习内容:

  1. Java方法的基本定义:
public static void 方法名(){
	方法体;
}

方法的定义与先后顺序无关
方法的定义不能产生嵌套包含关系
方法定义后不会执行,需要调用方法才能生效
eg:方法名(); 调用方法

  1. 顺序结构

顺序书写,根据代码书写的顺序,自上而下运行

  1. 选择结构

单if、if … else、if…else if…else

public static void ifDemo() {
        int age = 19;
        //if单分支语句
        if (age >= 18) {
            System.out.println("恭喜您已成年");
        }
        //if else 双分支语句 判断奇偶
        int num1 = 123;
        if (num1 % 2 == 0) {				//num%2 结果为数字类型,if后需要bollean类型,此处强制类型转换会发生错误
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }

        //if else if else 判断成绩
        int score = 100;
        if (score < 0 || score > 100) {
            System.out.println("数据错误");
        } else if (score >= 90 && score <= 100) {
            System.out.println("优秀");
        } else if (score >= 80 && score <= 89) {
            System.out.println("好");
        } else if (score >= 70 && score <= 79) {
            System.out.println("良好");
        } else if (score >= 60 && score <= 69) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
        //三元运算符替换为if else
        int a = 20, b = 456, max;
        //max = a > b ? a : b;

        if (a > b) {
            max = a;
        } else {
            max = b;
        }
        System.out.println("最大值为" + max);

        System.out.println("退出ifDemo");
    }
  1. 选择结构------switch语句
public static void switchDemo() {
        int num1 = 3;
        switch (num1) {
            case 1:
                System.out.println(1);
                break;
            case 2:
                System.out.println(2);
                break;
            default:
                System.out.println("数据错误");
                break;
        }

        System.out.println("退出switchDemo");
    }

switch语句注意事项 switch()中,只能是 :
基本数据类型 byte、short、int、char
引用数据类型 String、enum
case后面的数值不能重复
收尾用default,switch具有穿透性,末尾也要加上break
switch 语句灵活,语句顺序可以颠倒,break可以省略

  1. 循环结构-----for
public static void forDemo() {
        int i;
        for (i = 1; i <= 5; i++) {
            System.out.println("次数" + i);
        }
        System.out.println("退出forDemo");
    }

1 初始化语句(只执行一次) 2 条件判断 3步进语句 4循环体 (2 4 3循环执行,直到 2条件不满足跳出循环)

  1. 循环结构-----while/do…while
public static void whileDemo() {
        int i = 1;                          //初始化语句
        while (i <= 5) {                    //条件判断
            System.out.println("while" + i);  //循环体
            i++;                             //步进语句
        }
        int i2 = 1;
        
   		----------------------------------------
        do {
            System.out.println("do while" + i2);
            i2++;
        } while (i2 <= 5);

        System.out.println("退出whileDemo");
    }
}

7.do{} while();先执行一次,再进行判断,while与for先进行判断,不满足条件直接退出循环
8. break 是直接跳出循环,continue为跳出当前循环,继续进行下一轮循环
eg:只输出前三名信息 ,eg:例如1-10楼,去掉4楼

  1. while(true){}; 为死循环,循环体中没有break;在死循环后书写代码会导致错误,理论与实际上无法执行

10.循环的嵌套:

		//统计一天中有多少小时多少分钟
		for (i = 0; i <= 23; i++) {						//用于控制小时
            for (int j = 0; j <= 59; j++) {				//用于控制每小时内的分钟数
                System.out.println( i+"小时"+j + "分钟");
            }

        }
  1. 方法的定义

修饰符 返回值类型 方法名称(参数类型 参数名称,…){
方法体;
return 返回值;
}
注意:return 后的返回值类型,必须与方法名前的返回值类型相同

//判断两个数是否一样
 public static boolean numIsSame(int a, int b) {
        //return a == b ? true : false;
        return a == b;
    }
  1. 方法的三种调用类型
 		addSum(20,42);                      //单独调用
        System.out.println(addSum(20,45));  //打印调用
        int sum = addSum(12, 46);           //赋值调用

学习时间:

  • 3.30 上午8.30-下午7点

学习产出:

package March30th;

public class JavaTest {
    public static void main(String[] args) {
        System.out.println("以下是输出方法");
        //方法2,1的调用
        methodTwo();
        methodOne();
        //if 判断
        ifDemo();
        //switch 选择语句
        switchDemo();
        //for循环语句
        forDemo();
        //switch循环
        whileDemo();
    }

    //方法的定义,顺序无关
    public static void methodOne() {
        System.out.println("方法1");
    }

    public static void methodTwo() {
        System.out.println("方法2");
    }

    public static void ifDemo() {
        int age = 19;
        //if单分支语句
        if (age >= 18) {
            System.out.println("欢迎进来if语句");
        }
        //if else 双分支语句 判断奇偶
        int num1 = 123;
        if (num1 % 2 == 0) {
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }

        //if else if else 判断成绩
        int score = 100;
        if (score < 0 || score > 100) {
            System.out.println("数据错误");
        } else if (score >= 90 && score <= 100) {
            System.out.println("优秀");
        } else if (score >= 80 && score <= 89) {
            System.out.println("好");
        } else if (score >= 70 && score <= 79) {
            System.out.println("良好");
        } else if (score >= 60 && score <= 69) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
        //三元运算符替换为if else
        int a = 20, b = 456, max;
        //max = a > b ? a : b;

        if (a > b) {
            max = a;
        } else {
            max = b;
        }
        System.out.println("最大值为" + max);

        System.out.println("退出ifDemo");
    }

    public static void switchDemo() {
        //switch语句注意事项 switch()中,只能是 基本数据类型 byte、short、int、char 引用数据类型 String、enum,
        //case后面的数值不能重复,收尾用default,末尾也要加上break ,switch 语句灵活,语句顺序可以颠倒,break可以省略
        int num1 = 3;
        switch (num1) {
            case 1:
                System.out.println(1);
                break;
            case 2:
                System.out.println(2);
                break;
            default:
                System.out.println("数据错误");
                break;
        }

        System.out.println("退出switchDemo");
    }

    public static void forDemo() {
        // 1 初始化语句(只执行一次)  2 条件判断  3步进语句 4循环体 (2 4 3循环执行,直到 2条件不满足跳出循环)
        int i;
        for (i = 1; i <= 5; i++) {
            System.out.println("次数" + i);
        }
        System.out.println("退出forDemo");
    }

    public static void whileDemo() {
        int i = 1;                          //初始化语句
        while (i <= 5) {                    //条件判断
            System.out.println("while" + i);  //循环体
            i++;                             //步进语句
        }

        //do{} while();先执行一次,再进行判断
        int i2 = 1;
        do {
            System.out.println("do while" + i2);
            i2++;
        } while (i2 <= 5);

        System.out.println("退出whileDemo");
    }
}

package March30th;

public class Method {
    public static void main(String[] args) {
        //对于有返回值的方法,可以使用三者调用,void无返回值,只能使用打印调用
    /*    addSum(20,42);                      //单独调用
        System.out.println(addSum(20,45));  //打印调用
        int sum = addSum(12, 46);           //赋值调用
        System.out.println(sum);
     */
        printJuZhen();
        int num2 = method1(3, 4);
        System.out.println(num2);
        int num3 = addSum2(10, 50);
        System.out.println(num3);
        addSum3(10, 20);
        boolean result = numIsSame(10, 11);
        System.out.println(result);
        System.out.println(num1Add());
        printHelloWorld(5);
    }

    //求出两个数的和
    public static int addSum(int a, int b) {
        int sum = 0;
        sum = a + b;
        return sum;
    }

    //无参方法5行6列矩阵
    public static void printJuZhen() {
        int i, j;
        for (i = 0; i < 5; i++) {
            for (j = 0; j < 6; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    //有参求出两个数乘积
    public static int method1(int a, int b) {
        int num1 = a * b;
        return num1;
    }

    //计算两个数的和,并返还给赋值调用
    public static int addSum2(int a, int b) {
        int sum = a + b;
        return sum;
    }

    //无返回值求出两个数和
    public static void addSum3(int a, int b) {
        int sum = a + b;
        System.out.println(sum);
    }

    //判断两个数是否一致
    public static boolean numIsSame(int a, int b) {
        //return a == b ? true : false;
        return a == b;
    }
    //求出1-100累加和
    public static int num1Add(){
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        return sum;
    }
    //打印指定次数的HelloWorld
    public static void printHelloWorld(int count){
        for (int i = 0; i < count; i++) {
            System.out.println("HelloWorld");
        }
    }


}

package March30th;

public class JavaCirculateTest {
    public static void main(String[] args) {
        int sum = 0, i;
        //while 1-100偶数和
        i = 0;
        while (i <= 100) {
            if (i % 2 == 0) {
                sum += i;
            }
            i++;
        }
        System.out.println("1-100偶数和while" + sum);

        int sum2 = 0, i2;
        //do while
        i2 = 0;
        do {
            if (i2 % 2 == 0)
                sum2 += i2;
            i2++;
        } while (i2 <= 100);
        System.out.println("1-100偶数和do  while" + sum2);

        int sum3, i3;
        for (sum3 = 0, i3 = 0; i3 <= 100; i3++) {
            if (i3 % 2 == 0)
                sum3 += i3;
        }
        System.out.println("1-100偶数和for" + sum3);

        //break 跳出循环, continue 跳过当前循环
        //while(true) 死循环 循环体中没有跳出循环的break,后面不能书写其他语句,逻辑上永远执行不到其语句
        for (i = 0; i <= 23; i++) {
            for (int j = 0; j <= 59; j++) {
                System.out.println( i+"小时"+j + "分钟");
            }

        }
    }
}

观看视频到p72

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值