循环结构概述以及for语句基本格式 

循环结构的分类:

for,while,do...while
for语句的基本格式
for(初始化表达式;条件表达式;循环后的操作表达式) {
    循环体;
}

循环结构for语句练习水仙花数

public class for_shuixianhuashu {
    public static void main(String[] args) {
        int flag = 0;//记录水仙花数个数
        for (int i = 100;i < 1000;i++) {
            int g = i % 10;
            int s = i / 10 % 10;
            int b = i / 100 % 10;
            if (s * s * s + g * g * g + b * b * b == i) {
                System.out.println(i + "是水仙花数" );
                flag++;//个数加一
            }
        }
        System.out.println("水仙花数有" + flag + "个");//输出水仙花数的个数
    }
}

while循环的基本格式

while(判断条件语句) {
        循环语句体;
}
完整格式:
初始化语句;
while(判断条件语句) {
        循环体语句;
        控制条件语句;
}

循环结构while练习

public class while_ {
    public static void main(String[] args) {
        int flag = 0;
        int i=100;
        while(i<=999) {
            int g = i%10;
            int s = i/10%10;
            int b = i/10/10%10;
            if(g * g * g + s * s * s + b * b * b==i) {
                System.out.println(i);
                flag++;
            }
            i++;
        }
        System.out.println(flag);
    }
}

do...while语句格式

do {
        循环语句体;
}while(判断条件语句);
完整格式;
初始化语句;
do {
        控制条件语句;
}while(判断条件语句)
public class dowhile_ {
    public static void main(String[] args) {
        int i=1;
        do {
            System.out.println("i = "+i);
            i++;
        }
        while(i<=10);
    }
}

三种循环的区别

do...while至少循环一次 for和while则是先判断是否满足条件然后决定是否执行

for语句执行后变量会被释放不能再使用

while语句执行后的变量还可以继续使用

不知道用谁的时候就用for循环可以提高内存效率

!!!注意死循环

while(true)

for(;;)

利用循环嵌套输出四行五列的*

println和print的区别是println输出之后会直接换行

public class FOR__ {
    public static void main(String[] args) {
        for(int i = 0;i<4;i++){
            for(int j=0;j<4;j++){
                System.out.print("*");
            }
            System.out.println("*");
        }
    }

}

循环嵌套输出正三角形

public record FOR2() {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++) {
            for(int j=1;j<=i;j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 九九乘法表

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

控制跳转语句break

break只能在swich和循环中使用

控制跳转语句continue

只能在循环中使用,终止本次循环将进行下次循环

控制跳出语句标号

public record FOR2() {
    public static void main(String[] args) {
        outer: for(int i=1;i<=5;i++) { //a只要是合法标识符就可以
            inner:for(int j=1;j<=i;j++) {
                System.out.print("*");
                break outer; //跳出外部循环
            }
            System.out.println();
        }
    }
}

控制跳转语句return是用来返回方法的

return,break,continue的区别是

return是用来返回方法的break是用来跳出循环的continue是终止本次循环继续下次循环

方法是用来提高代码的复用性的完成特定功能的代码块

格式是:

修饰符 返回值类型 方法名(参数类型 参数名1;参数类型 参数名2;...){
        方法体语句;
        return返回值;
}

修饰符目前就用public static

实际参数:实际参与运算的参数

形式参数:就是方法定义上用于接收实际参数的

练习题

import java.util.Scanner;
public class methods_ {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int sum=add(a,b);
        System.out.println(sum);
    }
    public static int add(int a,int b) {
        int sum=a+b;
        return sum;
    }
}

方法的注意事项

一般不推荐单独调用(有返回值方法的单独调用,没有意义)eg:add(10,20)

输出调用,不能对结果进行进一步操作

赋值调用,推荐方案

参数之间用逗号隔开

方法调用时不用再传递数据类型

方法如果有明确的返回值一定要用return带回

练习:键盘录入两个数,返回两个数中的较大值

import java.util.Scanner;
public class methods_max {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int max=MAx(a,b);
        System.out.println(max);
    }
    public static int MAx(int a,int b){
        return a>b?a:b;
    }
}

判断两个整数是否相等

import java.util.Scanner;
public class methods_max {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        boolean m=deng(a,b);
        System.out.println(m);
    }
    public static boolean deng(int a,int b){
        return a==b;
    }
}

方法之星型输出以及录入

import java.util.Scanner;
public class methods_max {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int row=sc.nextInt();
        int column=sc.nextInt();
        print(row,column);
    }
    public static void print(int a,int b){
        for (int i=0;i<a;i++){
            for(int j=1;j<b;j++){
                System.out.print("*");
            }
            System.out.println("*");
        }
    }
}

方法重载概述和基本使用

方法名相同但是参数列表不同与返回值类型无关

分类:参数个数不同;参数类型不同;

二进制表示小数有的时候不太精确