java-4 流程控制

1.流程控制语句

  • 在一个程序执行过程中,各条件语句执行顺序对程序的结果是有直接影响的,也就是说程序的流程对运行结果有直接的影响,所以我们必须清楚每条语句执行流程,而且,很多时候我们要通过控制语句的执行顺序来实现我们要完成的功能:
  • 流程控制语句分类:顺序结构,选择结构,循环结构。

1.1 if语句

  • if语句

    • 语句格式:
    if (关系表达式){
    	语句体;
    }
    
    • demo
    package com.xjk;
    
    import java.util.Scanner;
    
    public class IfDemo {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("你是男的吗?(true/false)");
    		boolean re = sc.nextBoolean();
    		if (re) {
    			System.out.println("男人");
    		}
    		System.out.println("over");
    	}
    }
    
    • if条件判断2个数是否相等
    package com.xjk;
    
    import java.util.Scanner;
    
    public class IfDemo2 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入第一个整数:");
    		int a = sc.nextInt();
    		System.out.println("请输入第二个整数:");
    		int b = sc.nextInt();
            // 方式1
    		if (a == b) {
    			System.out.println("a等于b");
    		}
    		if (a != b) {
    			System.out.println("a不等于b");
    		}
            // 方式2
            String result = "a等于b";
            if (a!=b){
                result = "a不等于b";
            }
            System.out.println(result);
    	}
    }
    
    
    • 面试题1
    package com.xjk;
    
    public class IfDemo3 {
    	public static void main(String[] args) {
    		if (3==4)
    			System.out.println("333");
    			System.out.println("444");
    	}
    }
    
    // 打印结果:444
    // 原因if大括号是可以省略的,如果省略,那么控制到第一个分号结束
    
    
    
    
    • 面试题2:
    package com.xjk;
    
    public class IfDemo3 {
    	public static void main(String[] args) {
    		if (3==4)
    			System.out.println("333");
    			System.out.println("444");
    	}
    }
    // 333
    // 444
    
  • if...else语句

    • 语句格式:
    if (关系表达式) {
    	语句体1;
    } else {
    	语句体2;
    }
    
    • demo
    package com.xjk;
    
    public class IfDemo4 {
    	public static void main(String[] args) {
    		int a = 4;
    		int b = 5;
    		if (a == b) {
    			System.out.println("a等于b");
    		} else {
    			System.out.println("a不等于b");
    		}
    	}
    }
    
    • 键盘输入整数,判断是奇数还是偶数
    package com.xjk;
    
    import java.util.Scanner;
    
    public class IfDemo5 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入一个数:");
    		int a = sc.nextInt();
    		if (a%2==0) {
    			System.out.println("偶数");
    		}else {
    			System.out.println("奇数");
    		}
    	}
    }
    
    
  • if ... elseif ...else

    • 语句格式:
    if (关系表达式1) {
    	语句体1;
    } else if {
    	语句体2;
    } ...
    else {
    	语句体n;
    }
    
    • demo
    package com.xjk;
    
    import java.util.Scanner;
    
    public class IfDemo6 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入一个小数:");
    		double d = sc.nextDouble();
    		if (d>0) {
    			System.out.println("正数");
    		}else if (d == 0) {
    			System.out.println("为0");
    		} else if (d < 0) {
    			System.out.println("负数");
    		}
    	}
    }
    
    • 键盘输入月份,输出季节
    package com.xjk;
    
    import java.util.Scanner;
    
    public class IfDemo7 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入月份:");
    		int month = sc.nextInt();
    		if (month >=3 && month<= 5) {
    			System.out.println("春季");
    		} else if (month>=6 && month<=8) {
    			System.out.println("夏季");
    		}else if (month>=9 && month<=11) {
    			System.out.println("秋季");
    		}else if ( month == 12 | month == 1 | month ==2) {
    			System.out.println("冬季");
    		}
    	}
    }
    

1.2 switch语句

  • 语句格式:

    switch (表达式) {
    	case 常量1:
    		语句体1;
    		break;
    	case 常量2:
    		语句体2;
    		break;
    	...
    	default:
    		语句体n;
    		break;
    }
    
    • demo:日期输入
    package com.xjk;
    
    import java.util.Scanner;
    
    public class SwitchDemo1 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入数字(1-7):");
    		int day = sc.nextInt();
    		switch(day) {
    		case 1:
    			System.out.println("星期一");
    			break;
    		case 2:
    			System.out.println("星期二");
    			break;
    		case 3:
    			System.out.println("星期三");
    			break;
    		case 4:
    			System.out.println("星期四");
    			break;
    		case 5:
    			System.out.println("星期五");
    			break;
    		case 6:
    			System.out.println("星期六");
    		case 7:
    			System.out.println("星期日");
    			break;
    		default:
    			System.out.println("没有");
    			break;
    		}
    	}
    }
    
    • switch语句注意:
    1.case后面只能跟常量,不能跟变量。
    2.多个case值不能相同。
    3.case的顺序有没有要求,可以放任意位置。
    4.default 不一定放在最后位置,可以放任意位置
    5.default是可以省略的
    6.break是可以省略的,如果省略,代码会继续向下执行,不管下面case是否成功匹配,一直遇到break,或者switch执行结束。
    7.switch语句结束:遇到break,或者代码执行到switch语句最后。
    
    • 实现简单计算器
    package com.xjk;
    
    import java.util.Scanner;
    
    public class SwitchDemo2 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入一个整数:");
    		int a = sc.nextInt();
    		System.out.println("请输入符号(* - + /):");
    		String b = sc.next();
    		System.out.println("请输入一个整数:");
    		int c = sc.nextInt();
    		switch(b) {
    		case "+":
    			System.out.println(a+b+c+"="+(a+c));
    			break;
    		case "-":
    			System.out.println(a+b+c+"="+(a-c));
    			break;
    		case  "*":
    			System.out.println(a+b+c+"="+(a*c));
    			break;
    		case "/":
    			System.out.println(a+b+c+"="+(a/c));
    			break;
    		default:
    			System.out.println("输入不合法");
    			break;
    		}
    	}
    }
    
    

1.3 for循环

  • 循环语句可以在满足循环条件情况下,反复执行某一段代码。

  • 语法格式:

    for (初始化语句;判断条件语句;控制条件语句) {
    	循环体语句体;
    }
    
  • demo

    package branch;
    
    public class BranchDemo {
    	public static void main(String[] args) {
    		for (int i=0;i<5;i++) {
    			System.out.println("hello world");
    		}
    	}
    }
    
  • demo2

    // 打印1-10的和
    package branch;
    
    public class BranchDemo2 {
    	public static void main(String[] args) {
    		int sum = 0;
    		for(int i=1;i<=10;i++) {
    			sum += i;
    		}
    		System.out.println(sum);// 55
    	}
    }
    // 求1-100 奇数和和偶数和
    
    package branch;
    
    public class BranchDemo3 {
    	public static void main(String[] args) {
    		int odd = 0;
    		int even = 0;
    		for(int i=1;i<=100;i++) {
    			if (i%2 == 0) {
    				even += i;
    			} else {
    				odd += i;
    			}
    		}
    		System.out.println("奇数和为:"+odd);// 奇数和为:2500
    		System.out.println("偶数和为:"+even);// 偶数和为:2550
    	}
    }
    

1.4 while循环

  • 语句格式:

    while (判断条件语句) {
    	循环体语句;
    }
    
  • demo

    package branch;
    
    public class BranchDemo4 {
    	public static void main(String[] args) {
    		int i = 0;
    		while (i<100) {
    			System.out.println("这是:"+ i);
    			i++;
    		}
    	}
    }
    

1.5 do while循环

  • 语句格式

    do {
    	循环体语句;
    	控制条件语句;
    }while(判断条件语句);
    
    • do while 至少执行1次
  • demo

    package branch;
    
    public class BranchDemo5 {
    	public static void main(String[] args) {
    		int i = 0;
    		do {
    			System.out.println(i);
    			i++;
    		}while(i<100);
    	}
    }
    
    
  • 在键盘上输入一个数,如果是1,代表蛇吃到食物,得分为10,并继续,如果输入是非1数字,停止。输出分数:

    package branch;
    
    import java.util.Scanner;
    
    public class BranchDemo6 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("欢迎来到,贪吃蛇游戏");
    		System.out.println("请输入您的操作");
    		int result = sc.nextInt();
    		int score = 0;
    		while(result==1) {
    			score += 10;
    			System.out.println("请输入您的操作");
    			result = sc.nextInt();
    		}
    		System.out.println(score);
    	}
    }
    
  • 循环登录:

    package branch;
    
    import java.util.Scanner;
    
    public class BranchDemo7 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入密码:");
    		int secret = sc.nextInt();
    		while (secret!=1234) {
    			System.out.println("密码错误!");
    			System.out.println("请重新输入密码:");
    			secret = sc.nextInt();
    		}
    		System.out.println("登录成功");
    	}
    }
    
    
    • 但是在比较字符串内容,不能使用== 或 != 会出现问题,比较字符串通过equals比较
    package branch;
    
    import java.util.Scanner;
    
    public class BranchDemo7 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入密码:");
    		String secret = sc.nextLine();
    		while (!secret.equals("abc1234")) {
    			System.out.println("密码错误!");
    			System.out.println("请重新输入密码:");
    			secret = sc.nextLine();
    		}
    		System.out.println("登录成功");
    	}
    }
    // do while 版本
    package branch;
    
    import java.util.Scanner;
    
    public class BranchDemo7 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		String secret;
    		do {
    			System.out.println("请输入密码:");
    			secret = sc.nextLine();
    		} while (!"abc1234".equals(secret));
    		System.out.println("登录成功");
    	}
    }
    
    
  • 循环的嵌套

    package branch;
    
    public class BranchDemo8 {
    	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();
    		}
    	}
    }
    *
    **
    ***
    ****
    *****
    
  • 99乘法表

    package branch;
    
    public class BranchDemo8 {
    	public static void main(String[] args) {
    		for (int i=1;i<=9;i++) {
    			for (int j=1;j<=i;j++) {
    				System.out.print(j+"*"+i+"="+(i*j) + "	");
    			}
    			System.out.println();
    		}
    	}
    }
    
  • 打印字母A-Z

    package branch;
    
    public class BranchDemo9 {
    	public static void main(String[] args) {
    		for (char i='A'; i<='Z';i++) {
    			System.out.println(i);
    		}
    	}
    }
    

1.6跳转控制语句

  • break 结束单层循环

    package branch;
    
    public class BranchDemo10 {
    	public static void main(String[] args) {
    		for (int i=0;i<10;i++) {
    			if (i == 3) {
    				break;
    			}
    			System.out.println(i);
    		}
    		System.out.println("over");
    	}
    }
    // 0
    // 1
    // 2
    // over
    
  • continue 只能用在循环语句中,离开使用场景没有意义

    package branch;
    
    public class BranchDemo10 {
    	public static void main(String[] args) {
    		for (int i=0;i<10;i++) {
    			if (i == 3) {
    				continue;
    			}
    			System.out.println(i);
    		}
    		System.out.println("over");
    	}
    }
    // 0
    // 1
    // 2
    // 4
    // 5
    // 6
    // 7
    // 8
    // 9
    // over
    
  • return 结束整个方法,并返回方法的调用者

package branch;

public class BranchDemo10 {
	public static void main(String[] args) {
		for (int i=0;i<10;i++) {
			if (i == 3) {
				return;
			}
			System.out.println(i);
		}
		System.out.println("over");
	}
}
// 0
// 1
// 2
原文地址:https://www.cnblogs.com/xujunkai/p/13698035.html