JavaSE-流程控制

Scanner 包装类

Scanner 类的基本使用

  1. 创建实例对象
public class Demo{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
	}
}
  1. 判断是否输入
public class Demo{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.println(scanner.hasNext())
		System.out.println(scanner.hasNextLine())
	}
}
  1. 获取输入
public class Demo{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.println(scanner.next())  // 遇到空格就会停止
		System.out.println(scanner.nextLine())  // 直接输出整一行,可以获取空白
		System.out.println(scanner.nextInt()) // ...输出指定类型的输入
	}
}
  1. 关闭IO 流
public class Demo{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		scanner.close();
	}
}

顺序结构

  1. 说明:

语句与语句之间,框与框之间按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,顺序结构是任何一个算法的离不开的一种基本算法结构。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y3wUq2ZH-1653485195881)(imgclip.png "imgclip.png")]

选择结构

if(){}else if(){}else{}

  1. if 语句

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5B6OrDFP-1653485210624)(imgclip.png "imgclip.png")]

public class Main{
	public static void main(String[] args){
		int a = 10;
		if(a>=1){
			System.out.println("这个数大于等于1")
		}	
	}
}
  1. if - else 语句

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EiRLHOAM-1653485210630)(imgclip_1.png "imgclip_1.png")]

public class Main{
	public static void main(String[] args){
		int a = 0;
		if(a>=1){
			System.out.println("这个数大于等于1");
		}else{
			System.out.println("这个数小于1");
		}	
	}
}
  1. if - else - if 语句

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fANFJmjU-1653485210636)(imgclip_2.png "imgclip_2.png")]

public class Main{
	public static void main(String[] args){
		int a = 0;
		if(a>=1){
			System.out.println("这个数大于等于1");
		}else if(a==0){
			System.out.println("这个数等于0");
		}else{
			System.out.println("这个数小于1");
		}	
	}
}
  1. 嵌套 if 语句

switch([表达式]){case:…break;}

  1. 表达式类型
  • int、byte、short、char、String、枚举类。
  1. break 跳过中止执行
public class Main{
	public static void main(String[] args){
		int a = 10;
		switch(a){
			case 10:
				System.out.println("a 为 10");
			break;	
		}	
	}
}

循环结构

while(expression){expressionBody…}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fjzYgt7e-1653485251722)(imgclip.png "imgclip.png")]

// 循环累加 [0-100]
public class Main{
	public static void main(String[] args){
		int sum=0;
		int i=1;
		while(i<=100){
			sum+=i;
			i++;
		}
		System.out.println(sum);
	}
}
public class Main{
	public static void main(String[] args) {
        		Scanner scanner = new Scanner(System.in);
       	 	int com = new Random().nextInt(101);
        		System.out.println(com);
        		int num = scanner.nextInt();
        		while (num != com) {
            			if (num > com) {
                				System.out.print("too high");
            			} else {
                				System.out.print("too low");
            			}
            			num = scanner.nextInt();
        		}
        		System.out.print("输入正确:");
    	}
}

转换成 for:

public class Main{
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int com = new Random().nextInt(101);
        System.out.println(com);
        for (int num = scanner.nextInt(); num != com; num = scanner.nextInt()) {
            if (num > com) {

                System.out.print("too high");
            } else {
                System.out.print("too low");
            }
            System.out.print("");

        }
        System.out.print("输入正确:");
    }
}

无限循环:

public class Main{
	public static void main(String[] args) {
        /*
        1.输入密码 5次输入错误则退出 666666
        2.主界面
        3.查询余额
        4.取钱(钱充足)
        5.存钱(整钱100)
        */
        Scanner scanner =new Scanner(System.in);
        int errorCount=0;
        while(true){
            System.out.print("请输入密码:");
            int pwd=scanner.nextInt();
            if(pwd!=666666){
                errorCount++;   //5
                if(errorCount==5){
                    System.out.println("账号已被冻结!");
                    return;
                }
                System.out.println("密码错误,请重新输入!您还有"+(5-errorCount)+"次输入机会");
            }else{
                break;
            }
        }
        //主界面
        int summury=0;//总金额
        while(true){
            System.out.println("===========欢迎使用ATM自动存取款机===========");
            System.out.println("1.存钱");
            System.out.println("2.取钱");
            System.out.println("3.查询余额");
            System.out.println("0.退出");
            System.out.print("请选择办理的业务:");
            int choice=scanner.nextInt();
            switch(choice){
                case 1:
                    System.out.print("请输入存款金额:");
                    int inmoney=scanner.nextInt();
                    summury+=inmoney;
                    System.out.println("存钱完毕!");
                    break;
                case 2:
                    System.out.println("你的余额为"+summury+"元");
                    System.out.print("请输入取款金额:");
                    int outmoney=scanner.nextInt();
                    if(outmoney>summury){
                        System.out.println("您的余额不足!");
                    }else{
                        summury-=outmoney;
                        System.out.println("取款完毕!");
                    }
                    break;
                case 3:
                    System.out.println("你的余额为"+summury+"元");
                    break;
                case 0:
                    System.out.println("谢谢使用,请收好您的卡片!");
                    return;
            }
        }
    }
}

do{expressionBody…}while(expression)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-elVEw3sO-1653485251724)(imgclip_1.png "imgclip_1.png")]

public class Main {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println(i);
            i++;
        } while (i <= 10);
    }
}

无限循环:

public class Main {
    public static void main(String[] args) {
        do {
            System.out.println("infinitive do while loop");
        } while (true);
    }
}

while 与 dowhile循环的区别在于while先判断后执行,而dowhile是先执行后判断,也就是说dowhile永远都会执一次。

for(声明语句(初始化);循环范围(布尔表达式);变量更迭(更新)){expressionBody…}

  1. 简单 for 循环

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d8UcXx1q-1653485251728)(imgclip_2.png "imgclip_2.png")]

public class Main {  
    public static void main(String[] args) {  
        for(int i=1;i<=10;i++){  
            System.out.println(i);  
        }  
    }  
}
  1. forEach 循环(增强for 循环)

常用于数组相关遍历。

public class Main  {
    public static void main(String[] args) {
        int arr[] = { 12, 23, 44, 56, 78 };
        for (int i : arr) {
            System.out.println(i);
        }
    }
}
  1. 标记 for 循环

为此,在for循环之前使用标签。它是有用的,如果在嵌套for循环中,可以使用break/continue指定循环。
通常,break和continue关键字断开/继续最内循环。

public class Main  {
    public static void main(String[] args) {
        aa: for (int i = 1; i <= 3; i++) {
            bb: for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break aa;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

输出:

1 1
1 2
1 3
2 1

如果使用break bb;它将打断内循环,这是任何循环的默认行为。

public class Main {
    public static void main(String[] args) {
        aa: for (int i = 1; i <= 3; i++) {
            bb: for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break bb;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

输出:

1 1
1 2
1 3
2 1
3 1
3 2
3 3
  1. 无限 for 循环
public class Main {
    public static void main(String[] args) {
        for (;;) {
            System.out.println("infinitive loop");
        }
    }
}

输出:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

for 循环打印乘法表

public class Demo{
	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')
			}
			System.out.println()
		}
	}
}

break continue

  • break:跳出循环,中止循环。

  • continue:中止某次循环的过程,接着进行下一次循环判定。

  • goto:实际上就是标签型for 循环。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值