从零学Java:学习笔记01-Java的流程控制结构语句

流程控制:

顺序结构:

它是一个应用程序(程序计数器)能够自上往下运行的最基本结构、先执行第一行代码、再执行第二行代码、以此类推。

public class Main{
	public static void main(String [] args){
		System.out.println("我先执行");
		System.out.println("我第二执行");
		System.out.println("我最后执行");
	}
}

选择结构:

选择结构表示的是“如果…”“就…”的逻辑、应用程序通过选择结构能够按照特定的条件决定执行指定的代码、选择结构用于判断条件、根据判断的结果控制程序的流程。

单选择结构:

基本语法:
if(布尔表达式){
执行的代码
}
在这里插入图片描述

public class Main{

 	public static void main(String [] args){
        // 产生三个1~6之间的随机数
        int i = (int)(6*Math.random())+1;
        int x = (int)(6*Math.random())+1;
        int j = (int)(6*Math.random())+1;
        // 三个骰子的点数进行相加
        int count = i+x+j;
        
        System.out.println(count);
        // if选择结构
        if(count>=15){ // 如果骰子点数大于15
            System.out.println("今天手气不错哦~");
        }
        if(count<15&&count>=10){ // 如果骰子点数小于15并且骰子点数大于等于10
            System.out.println("今天手气一般般");
        }
        if(count<10){ // 如果骰子点数小于10
            System.out.println("今天手气不好哦");
        }
        
    }
}

双选择结构:

双选择结构表示的是“如果”“就”,“否则”"就"的逻辑、相比单选择结构、它能够让应用拥有更为严谨的处理能力、如果就的逻辑使用if代码块表示、否则就的逻辑则是用else代表块进行表示。
在这里插入图片描述

public class Main{
		
		public static void main(String [] args){
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入您的数学成绩分数:");
			int score = scanner.nextInt();
			// 判断分数是否合格
			if(score >= 60){
				System.out.println("及格");
			}else{
				// if判断语句不成立程序则走else语句
				System.out.println("不及格");
			}
		}
}

多选择结构:

多选择结构表示的是“如果”“就”,“如果”“就”,“”…多条件的逻辑、相比双选择结构、它拥有更多的条件判断、应用程序的执行分支越多、提高程序的丰富多变性及严谨性。
在这里插入图片描述

public class Main{
    public static void main(String [] args){
        Scanner scaner = new Scanner(System.in);
        System.out.println("请输入您的实际年龄:");
        int age = scaner.nextInt();
        System.out.print("你今年:"+age+"岁,是个");
        // 判断用户输入的年龄
        if(age < 15){
            System.out.println("小孩子哦");
        }else if(age < 30){ // 需要注意的是 这里的age<25相当于>15并且<25 因为如果15的条件判断应该走上面的分支 而不是这里 下面的条件也是如此
            System.out.println("青年人哦");
        }else if(age < 45){
            System.out.println("中年人哦");
        }else{
            System.out.println("老年人哦");
        }
    }
}

switch多选择结构:

switch多选择结构也是日常程序开发中常见的选择结构之一、但它与if else if else多选择结构不同的是、switch多 选择结构只能用作于等值的判断、虽然if else if else多选择结构也能够用于等值判断、但它也能够实现区间判断的情况、而使用switch无法实现区间判断的需求。
在这里插入图片描述
需要注意的是、在jdk1.5之前、switch语句的case标签必须是整数或枚举、不能是字符串、在jdk1.7之后case标签支持字符串(String类型)。
每一个case语句后都需要break语句结束、否则会出现case穿透的问题。

public class Main{
		
		public static void main(String [] args){
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入月份:");
			// 获取用户输入的内容
			int month = scanner.nextInt();
			switch(month){
			case 1: // 该值必须和switch中的值类型保持一致
				System.out.println("过新年啦!");
				break;// 避免case穿透问题
			case 2:
				System.out.println("开春啦!");
				// 这里没有break语句、会发生case穿透 如果用户输入的是2 它会连同default语句一并执行、而switch碰到break会终止、所以最好个case都要加上break进行控制
			default:
				System.out.println("其他月份");
		}
	}
}

循环结构:

猜你喜欢

转载自blog.csdn.net/weixin_38858343/article/details/83241964