07-13

一、Java流程控制

1.1 选择结构

1.1.1 单选择结构

- if(布尔表达式){
- }

equals()用来判断字符串

package com.baidu;

import org.w3c.dom.ls.LSInput;

import java.nio.Buffer;
import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        Scanner fg=new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        if(fg.nextLine().equals("hello")){
            System.out.println("您输入的是hello");
        }
        System.out.println("程序结束");
        fg.close();
    }
}

1.1.2双选择结构

if(){
    
}else{
    
}

1.1.3 if 多选择结构

if(){
    
}else if(){
    
}else if(){
    
}else {
    
}//一旦有一个else if 为true,其他的if就会跳过执行

1.1.4 switch 多选择结构

switch与if语句的区别在于,if在于判断一个区间,而switch则是匹配一个具体的值。

package com.baidu;

public class SwitchDemo1 {
    public static void main(String[] args) {
        char x='B';
        switch (x){
            case 'A':
                System.out.println("good");
                break;
            case 'B':
                System.out.println("common");
                break;
            default:
                System.out.println("您挂科了");
        }
    }
}
//不加break,会产生case穿透

通过字节码文件反编译可以看出,switch语句是通过hashcode(哈希编码)判断的。

1.2 循环结构

1.2.1 while循环

while(布尔表达式){
    循环内容
}

1.2.2 do…while 循环

do{
    循环内容
}while(布尔表达式)

1.2.3 for 循环

for(初始化;布尔表达式;更新){
    代码语句
}

100.for , idea会自动生成 for(int i=0;i<=100;i++){

}

死循环:for(; ; ){ }

package com.baidu;

public class Sum100 {
    public static void main(String[] args) {
        //用for循环输出1-100之间能被5整除的数,并且每行打印三个
        for (int i = 0; i <= 100; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%15==0){
                System.out.println();//换行,等价于System.out.print("\n")
            }
        }
    }
}

  • print() : 输出不会换行

  • printLn():输出完全换行

package com.baidu;
//打印九九乘法表
public class Multiplication {
    public static void main(String[] args) {
        for (int i = 1; i <=9; i++) {
            for(int j=1;j<=i;j++){
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
                if(i==j){
                    System.out.println();
                }
            }
        }
    }
}

上面代码输出为:

1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81

1.2.4 增强 for 循环

主要是遍历数组和集合

package com.baidu;

public class Demo4 {
    public static void main(String[] args) {
        int[] numbers={10,20,30,60};//int[]表示数组类型
        for(int x:numbers){
            System.out.println(x);
        }
    }
}

结果为:

10
20
30
60

Process finished with exit code 0

1.2.5 break 和 continue

1.3 练习

1.3.1打印三角形

package com.baidu;

public class Triangle {
    public static void main(String[] args) {
        for (int i = 1; i <=5; i++) {
            for (int j = 1; j <= (5-i); j++) {
                /*也可以使用 
                for(int i=5;j>=i;j--)
                */
                System.out.print(" ");
            }
            for (int x = 1; x <=(2*i-1); x++) {
                System.out.print("*");
            }
            System.out.println();

        }
    }
}

    *
   ***
  *****
 *******
*********

打断点处的句子不执行,只执行到断点处的前一句

二 Java 方法详解

2.1 什么是方法

package method;

public class Sum {
    public static void main(String[] args) {
        int a=add(1,3);
        System.out.println(a);//输出结果为4
    }
    public static int add(int a,int b){
        return a+b;
    }
}

2.2 方法的参数和调用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DvakEGMx-1626180996613)(C:\Users\77647\AppData\Roaming\Typora\typora-user-images\image-20210713144115053.png)]

  • return一般要放在方法的最外层括号,否则会报错.
  • return也可以写在程序中间,用来终止程序,即表明程序已经有返回值.
  • java都是值传递

2.3方法的重载

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9AaXr3qC-1626180996617)(C:\Users\77647\AppData\Roaming\Typora\typora-user-images\image-20210713151919647.png)]

2.4可变参数

可变参数类型的设置格式为:

修饰符 返回值 方法名(int…参数名){

​ 方法体

​ return 返回值;

}

package com.baidu;//输出用户输入的所有数字中的最大值

public class Max {
    public static void main(String[] args) {
        printMax(10,20,55,60,70);

    }
    public static void printMax(int...x){
        if(x.length==0){
            System.out.println("没有输入参数");
            return;
        }
        int Max=x[0];
        for (int i = 0; i < x.length; i++) {
            if(x[i]>Max){
                Max=x[i];
            }
        }
        System.out.println("您输入的数中,最大值为:"+Max);
    }
}

您输入的数中,最大值为:70

2.5 递归

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-znp3kFLv-1626180996620)(C:\Users\77647\AppData\Roaming\Typora\typora-user-images\image-20210713205423623.png)]

stack : 栈

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7I3TmTvZ-1626180996622)(C:\Users\77647\AppData\Roaming\Typora\typora-user-images\image-20210713204929519.png)]

上图表示递归的本质就是依次调用,然后依次返回.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值