Java流程控制

目录

知识目标

3.1 引子

3.2 语句、语句块和作用域

3.3 分支控制语句

3.3.1 if-else 结构

3.3.2 嵌套if-else结构

3.3.3 Switch结构

3.4 循环控制语句

3.4.1 while结构

3.4.2 do-while结构

3.4.3 for循环

3.4.4 多重for循环

3.5 转移控制语句

3.5.1 break

3.5.2 continue

3.5.3 return


知识目标

  1. 了解语句、语句块和作用域的概念;

  2. 掌握分支控制结构if - else 和switch的使用;

  3. 掌握循环控制语句结构while、do - while 和 for的使用;

  4. 掌握转移控制语句 break 、 continue 和return的使用

3.1 引子

import java.util.Scanner;

public class Example3_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/**
		 * 判断是否是闰年
		 */
		
		String string;
		Scanner scanner = new Scanner(System.in);
		int year;
		
		do {
			System.out.println("请输入年份:");
			year = scanner.nextInt();
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				System.out.println(year + "是闰年");
			} else {
				System.out.println(year + "不是闰年");
			}
			System.out.println("是否继续?y  OR  n");
			string = scanner.next();
		} while (string.equalsIgnoreCase("y"));
		
		System.out.println("程序结束");
	}
}

        if-else 语句块属于分支结构;do-while语句块属于循环结构。Java语句,语句块和作用域

3.2 语句、语句块和作用域

        一段完整的Java程序由若干条语句组成,每条语句都是一条完整的执行单元,以分号结束。

        语句块由一对大括号{}和其中的语句组成,语句块中的语句可以为一行、多行或另一个语句块,也可以一个都没有,成为空语句块。语句块中的语句仅为一行时,{}可以省略。

        每个语句块定义了一个作用域,在作用域里定义的都是局部变量,局部变量只有在语句块中具有可见性。语句块可以嵌套,没创建一个语句块就创建了一个新的作用域。语句块嵌套时外层语句块的作用域包含内部语句块的作用域,即外层作用域的变量可以在内层使用,反之不行。

public class Example3_1_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		{
			int a = 1;
			int b = 2;
			{
				System.out.println("a: " + a);
				System.out.println("b: " + b);
			}
		}
	}

}

         错误示例:内层变量在外层不能访问到


public class Example3_1_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		{
			int a = 1;
			int b = 2;
			{
				System.out.println("a: " + a);
				System.out.println("b: " + b);
				
				int c = 9;
				
			}
			
			System.out.println("c: " + c);
		}
	}

}

 

3.3 分支控制语句

        分支结构使得程序可以跳过某些语句转而执行特定的语句。分支结构包括if-else和switch。

3.3.1 if-else 结构

        if-else结构存在3种形式

// 第一种
if (布尔表达式){
    语句块一;
} else {
    语句块二;
}

        判断学生成绩是否合格

import java.util.Scanner;

public class Example3_2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩:");
		
		double score = sc.nextDouble();
		
		if (score >= 60) {
			System.out.println("成绩合格。");
		}else {
			System.out.println("成绩不合格。");
		}
	}

}

// 第二种
if(表达式){
    语句块一;
}
import java.util.Scanner;

public class Example3_2_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩:");
		
		double score = sc.nextDouble();
		
		if (score >= 60) {
			System.out.println("考试通过!!!!");
		}
	}
}

// 第三种
if (布尔表达式1){
    语句块一;
} else if(布尔表达式2){
    语句块二;
} else {
    语句块三;
}
import java.util.Scanner;

public class Example3_2_2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩:");
		
		double score = sc.nextDouble();
		
		if (score < 60) {
			System.out.println("成绩不合格。");
		}else if(score <= 70){
			System.out.println("成绩为 中。");
		}else if(score <= 80){
			System.out.println("成绩为 良。");
		}else if(score <= 90){
			System.out.println("成绩为 优。");
		}else {
			System.out.println("秀儿。");
		}
	}
}

3.3.2 嵌套if-else结构

if(表达式1){
    if(表达式2){
    	语句块;
	}else{
        语句块;
    }
} else {
    if(表达式2){
    	语句块;
	}else{
        语句块;
    }
}

import java.util.Scanner;

public class Example3_2_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩:");
		
		double score = sc.nextDouble();
		
		if (score >= 0 && score <= 100) {
			if (score < 60) {
				System.out.println("成绩不合格。");
			}else if(score <= 70){
				System.out.println("成绩为 中。");
			}else if(score <= 80){
				System.out.println("成绩为 良。");
			}else if(score <= 90){
				System.out.println("成绩为 优。");
			}else {
				System.out.println("秀儿。");
			}
		} else {
			System.out.println("输入错误");
		}
	}
}

 

3.3.3 Switch结构

        格式:

        switch (布尔表达式) {
            case 常量1:{
                break;
            }
            case 常量2:{
                break;
            }
        
             ......   
        
            default:{
                break;
            }
        }
import java.util.Scanner;

public class Example3_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩:");
		
		double score = sc.nextDouble();
		
		switch ((int)score / 10) {
			case 10:{
				System.out.println("秀儿:");
				break;
			}
			case 9:{
				System.out.println("优:");
				break;
			}
			case 8:{
				System.out.println("良:");
				break;
			}
			case 7:{
				System.out.println("中:");
				break;
			}
			case 6:{
				System.out.println("差:");
				break;
			}
			default:{
				System.out.println("输入错误或者不合格:");
				break;
			}
		}
	}

}

3.4 循环控制语句

3.4.1 while结构

        格式:

        while(布尔表达式){
            循环体;
        }

            while 结构的执行顺序为:先计算表达式的值,如果为 true ,就执行循环体内的语句;如果为 false ,就结束循环。每进行下一次循环前,要重新计算表达式的值,知道为 false 为止。
            在循环体内,必须存在一个控制循环执行有限次数的循环变量。

public class Example3_4 {

	public static void main(String[] args) {
		/**
		 * 计算1到100的和
		 */
		int i = 1;
         int sum = 0;
		while (i<=100) {
			sum += i;
			i++;
		}
		System.out.println(sum);
	}
}

         算法?什么是算法?

        int i = 1;
        int sum = 0;
        int n = 100;
        
        sum = (i + n) * n /2;

3.4.2 do-while结构

        格式:

        do{
            循环体;
        }while(布尔表达式)
public class Example3_4_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int i = 1;
		int sum = 0;
		
		do {
			sum += i;
			i++;
		} while (i<=100);
		
		System.out.println(sum);
	}
}

 

3.4.3 for循环

        格式:

        for(表达式1;表达式2;表达式3){
            循环体;
        }
public class Example3_4_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		

		int sum = 0;
		
		for (int j = 1; j <= 100; j++) {
			sum += j;
        }	
		System.out.println(sum);
	}
}

3.4.4 多重for循环

        循环是可以嵌套的

public class Example3_4_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		

		int sum = 0;
		
		for (int j = 1; j <= 50; j++) {
			for (int i = 1; i <= 50; i++) {
				sum += 1;
			}
		}
		
		System.out.println(sum);
	}

}

3.5 转移控制语句

3.5.1 break

        跳出当前循环

public class Example3_5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub


		int sum = 0;
		
		for (int j = 1; j <= 50; j++) {
			for (int i = 1; i <= 50; i++) {
				sum += 1;
				if (j == 25) {
					break;
				}
			}
		}
		
		System.out.println(sum);
		
	}

}

3.5.2 continue

        结束本次循环,执行下次循环

public class Example3_5_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub


		loop:
			for (int i = 2,k = 0; i < 100; i++) {
				for (int j = 2; j <= Math.sqrt(i); j++) {
					if (i % j == 0) {
						continue loop;
					}
				}
				
				System.out.print(i + "  ");
				k++;
				if (k % 5 == 0) {
					System.out.println();
				}
			}
		
		
	}

}

3.5.3 return

        返回值,用于方法

        return 表达式;
public class Example3_5_2 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		String string = getName("xiaoming");
		System.out.println(string);
	}
	
	
	public static String getName(String name) {
		return name;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铲屎官白茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值