Java流程控制


JavaDoc

javadoc命令是用来生成自己API文档的

参数信息:

  • @author 作者名
  • @version 版本号
  • @since 指明需要最早使用的jdk版本
  • @param 参数名
  • @return 返回值情况
  • @throws 异常抛出情况

(面向百度编程!!! 如何在idea中生成JavaDoc文档?)


Java流程控制

顺序结构

Java的基本结构就是顺序结构,它是任何一个算法都离不开的一种基本算法结构。

选择结构*

  • if单选择结构

if(布尔){}

  • if双选择结构 if-else

        public static void main(String[] args) {
            //考试分大于60分就是及格,小于60分就是不及格
            Scanner scanner=new Scanner(System.in);
            int score= scanner.nextInt();
            if(score>60){
                System.out.println("及格");
            }else {
                System.out.println("不及格");
            }
            scanner.close();
        }
    
  • if多选择结构 if--else if,以else结尾

        public static void main(String[] args) {//程序不严谨!!!
            Scanner scanner=new Scanner(System.in);
            int score= scanner.nextInt();
            if(score>=90){
                System.out.println("优秀");
            }else if(score>=70&&score<90){
                System.out.println("良好");
            }else if (score>=60&&score<70){
                System.out.println("及格");
            }else{
                System.out.println("不及格");
            }
            scanner.close();
        }
    
        public static void main(String[] args) {
            Scanner scanner=new Scanner(System.in);
            int score= scanner.nextInt();
            if(score>=90&&score<=100){//注意成绩的有效性,程序要严谨
                System.out.println("优秀");
            }else if(score>=70&&score<90){
                System.out.println("良好");
            }else if (score>=60&&score<70){
                System.out.println("及格");
            }else if(score>=0&&score<60){
                System.out.println("不及格");
            }else {
                System.out.println("成绩不合法");
            }
            scanner.close();
        }
    
  • 嵌套的if结构

    所有的流程控制都可以嵌套,互不影响。

  • switch--case多选择结构

    支持byte,char,int,jdk7之后支持string字符串格式(字符的本质还是数字)。

        public static void main(String[] args) {
            //case穿透(每写一个case需要加上break)  //switch匹配一个具体的值
            char grade ='c';
            switch (grade){
                case 'a' :
                    System.out.println("优秀");
                    break;//可选,如果对应选项里没加break,会输出所有case直到下个break出现或者程序结束为止
                case 'b' :
                    System.out.println("良好");
                    break;
                case 'c' :
                    System.out.println("及格");
    
                case 'd' :
                    System.out.println("不及格");
                    break;
                default://可选
                    System.out.println("未知等级");
            }
        }
    

循环结构

1.while循环 2.do。。。while循环 3.for循环

while循环

while(布尔表达式){

//循环内容

大多数情况会让循环停止,需要一个表达式失效的方式来结束循环。

do...while循环

  • while循环如果不满足条件,则不能进入循环;而do...while循环即使不满足条件,也至少执行一次
  • do...while先执行后判断,while先判断后执行。

do {

? //代码语句

}while(布尔表达式)

For循环*

  • for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

for(初始化;布尔表达式;更新){

? //代码语句

}

    public static void main(String[] args) {
        int a = 1;//初始化条件

        while (a<=100){//条件判断
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("while循环结束");

        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束");
        
        /*
        关于for循环有以下几点说明:
        
        最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
        然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始循环体后面的语句。
        执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
        再次检查布尔表达式,循环执行上面的过程。
         */
        
        //死循环
        for (; ; ) {
            
        }

练习1:计算0到100之间的奇数的和与偶数的和

    public static void main(String[] args) {
        //练习1:计算0到100之间的奇数的和与偶数的和

        //自写方法1
        int sum=0;
        int j = 0;
        for (int i = 1; i < 100; i=i+2) {
            j+=2;
            sum=sum+i+j;
        }
        System.out.println(sum);

        //自写方法2
        int jiSum = 0;//奇数和
        int ouSum = 0;//偶数和
        for (int i = 0; i <= 100; i+=2) {
            ouSum+=i;
        }
        for (int i = 1; i < 100;  i=i+2) {
            jiSum+=i;
        }

        System.out.println(jiSum+ouSum);

        //视频方法
        int oddSum = 0;//奇数和
        int evenSum = 0;//偶数和

        for (int i = 0; i <= 100; i++) {
            if (i%2!=0){//奇数
                oddSum+=i;
            }else {//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }

练习2:用while循环或者for循环输出1-1000之间能被5整除的数,并且每行输出3个

    public static void main(String[] args) {
        //练习2:用while循环或者for循环输出1-1000之间能被5整除的数,并且每行输出3个
        int count = 0;
        for (int i = 1; i < 1000; i++) {
            if (i%5==0){
                System.out.print(i+"  ");
                count++;
                if(count%3==0){//if要嵌套写在里边,否则在第三个数到第四个数之间会输出5遍换行
                    System.out.println();//因为这五个数之间count都符合要求
                }
            }
        }
    }

练习3:九九乘法表

    public static void main(String[] args) {
        //练习3:九九乘法表
        int sum;
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < i+1; j++) {
                sum=i*j;
                if(sum<10){
                    System.out.print(j+"*"+i+"="+sum+"   ");
                }else {
                    System.out.print(j+"*"+i+"="+sum+"  ");
                }
                if (i==j){
                    System.out.println();
                }
            }
        }
    }

1.先打印乘法表第一列
2.把固定的1再用循环抱起来
3.用变量j替换固定的1,实现9X9
4.去掉重复项,i<=j
5.调整样式

(思想很重要!!!)

        //方法2
		for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }

image-20200810224016206

增强for循环

主要用于数组或集合。

for(声明语句:表达式)

? //代码句子

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。作用域限定在循环语句块,其值与此时的数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

    public static void main(String[] args) {
        int[] numbers={10,20,30,40,50};
        
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
        
        //遍历数组元素
        for (int x:numbers){
            System.out.println(x);
        }
    }

break continue

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环剩余的语句。(break也在switch语句中使用)
  • continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否循环的判定。

整体练习

    public static void main(String[] args) {
        //打印5行的三角形
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }