Java初了解(四)--- 流程控制+经典案例

淩亂°似流年 2023-07-18 06:54 165阅读 0赞

补充:运算符(自增自减)

  1. 自增自减运算符存在于Java/Python等高级语言中,它的作用是在运算结束前(前置自增自减运算符)或后(后置自增自减运算符)将变量的值加(或减)一。
  2. 相较于这些语言中的+=和-=运算符,自增运算符更加简洁,且可以控制效果作用于运算之前还是之后,具有很大的便利性。

    package cn.daniel.day03;

    //自增自减运算符
    public class TestAdd {

    1. public static void main(String[] args) {
    2. //++ 单独使用
    3. int a = 10;
    4. int b = 10;
    5. a++;//相当于a=a+1
    6. ++b;
    7. System.out.println(a);//11
    8. System.out.println(b);//11
    9. //++ 混合使用
    10. int c = 10;
    11. int d = c++;//等同于,r=b,b=b+1,注意++后执行
    12. System.out.println(d);//10
    13. int e = 10;
    14. int f = ++e;//注意++先执行
    15. System.out.println(f);//11
    16. // -- 单独使用
    17. int g = 10;
    18. int h = 10;
    19. g--;//相当于o=o-1
    20. --h;
    21. System.out.println(g);//9
    22. System.out.println(h);//9
    23. // -- 混合使用
    24. int i = 10;
    25. System.out.println(i--);//10 --后执行
    26. int n = 10;
    27. System.out.println(--n);//9 -- 先执行
    28. }

    }

前置后置区别的举例

  1. a = i++;//先赋值,后计算
  2. 相当于 a = i; i++;
  3. a = ++i;//先计算,后赋值
  4. 相当于 i++; a = i;
  5. (--同理)

二、异常处理

异常处理使用try、catch和finally关键字来尝试可能未成功的操作,处理失败,以及在事后清理资源。

不处理异常案例演示:

  1. package cn.daniel.day03;
  2. public class TestTryCatch {
  3. public static void main(String[] args) {
  4. //人为制造异常 2/0(报错)
  5. int n = 2/0;//如果不管,抛出异常给调用者,main函数,抛出异常
  6. System.out.println(n);
  7. }
  8. }

控制台输出:

  1. Exception in thread "main" java.lang.ArithmeticException: / by zero
  2. at cn.daniel.day03.TestTryCatch.main(TestTryCatch.java:6)

异常的两种处理方式:

  • throws 抛出异常,自己不处理
  • try-catch 自己处理异常
1、使用try、catch、finally关键字捕获异常:
  • 语法格式为1:

    try{

    1. //可能会出现异常的代码

    }catch(ParseException e){

    1. //捕获执行的代码

    };

案例展示:

  1. package cn.daniel.day03;
  2. public class TestTryCatch {
  3. public static void main(String[] args) {
  4. try { //自己管理异常
  5. System.out.println("进入了:try");
  6. //写可能发生异常的代码
  7. int n = 2/0;
  8. System.out.println("执行完了:try");
  9. } catch (Exception e) {
  10. // TODO: handle exception
  11. System.out.println("进入了:catch");
  12. //获取出错,打印错误信息
  13. System.out.println(e.getMessage());
  14. System.out.println("执行完了catch");
  15. }
  16. System.out.println("try~catch执行完毕!!!");
  17. }
  18. }

控制台显示:

  1. 进入了:try
  2. 进入了:catch
  3. / by zero
  4. 执行完了catch
  5. try~catch执行完毕!!!
  • 语法格式为2:

    try{

    1. //可能会出现异常的代码

    }catch(Exception e){

    1. //捕获执行的代码

    }finally{

    1. //不管是否发生异常都要执行的代码

    }

案例展示:

  1. package cn.daniel.day03;
  2. public class TestTryCatchFinally {
  3. public static void main(String[] args) {
  4. try {
  5. System.out.println("try start...");
  6. int i = 1/0;
  7. System.out.println("try end...");
  8. } catch (Exception e) {
  9. // TODO: handle exception
  10. System.out.println("catch start...");
  11. System.out.println(e.getMessage());
  12. System.out.println("catch end...");
  13. }finally {
  14. //try 正确还是错误,都会执行这里的代码
  15. System.out.println("finally....");
  16. }
  17. System.out.println("finash");
  18. }
  19. }

控制台输出:

  1. try start...
  2. catch start...
  3. / by zero
  4. catch end...
  5. finally....
  6. finash

关键词try后的一对大括号将一块可能发生异常的代码包起来,称为监控区域。Java方法在运行过程中出现异常,则创建异常对象。将异常抛出监控区域之外,由Java运行时系统试图寻找匹配的catch子句以捕获异常。若有匹配的catch子句,则运行其异常处理代码,try-catch语句结束。

2、使用throws关键字声明抛出异常:

谁调用抛给谁去解决
分成两步:

  1. 抛异常 throw new Exception(“”)
  2. 接异常 throws Exception

案例展示:

  1. package cn.daniel.day03;
  2. public class TestThrowThrows {
  3. //在main函数来处理,它内部错误,接盘
  4. public static void main(String[] args) throws Exception {
  5. try {
  6. int i = 1/0;
  7. } catch (Exception e) { // 抛出异常
  8. //创建一个新的异常对象Exception,括号里写错误信息
  9. throw new Exception("系统出错了,请联系管理员");
  10. }
  11. }
  12. }

控制台输出:

  1. Exception in thread "main" java.lang.Exception: 系统出错了,请联系管理员
  2. at cn.daniel.day03.TestThrowThrows.main(TestThrowThrows.java:10)

三、程序流程控制

  • 分支结构:根据判断,有些代码执行,有些代码不执行
  • 顺序结构:代码依次从上往下执行
  • 循环结构:反复执行一段代码

(一)分支结构

  • if
    结构式:

    if(条件表达式){

    1. 语句块;

    }

  • if~else:条件是互斥的
    结构式:

    if(条件表达式){

    1. 语句块1;

    }else{

    1. 语句块2;

    }

  • if~else if~else
    结构式:

    if (条件表达式1) {

    1. 语句块1;
    2. } else if(条件表达式2) {
    3. 语句块2;
    4. }else {
    5. 语句块3;
    6. }

测试用例:(使用单元测试演示)

  1. package cn.daniel.day03;
  2. import java.util.logging.Level;
  3. import org.junit.Test;
  4. public class TestIfElse {
  5. @Test
  6. public void testif() {
  7. //需求:判断男女
  8. boolean isMan = true;//默认值是false
  9. System.out.println(isMan);
  10. if (isMan) {
  11. System.out.println("男人");
  12. }
  13. }
  14. @Test
  15. public void testIfElse() {
  16. //需求:判断男女
  17. //if~else:条件是互斥的,要么真,要么假
  18. // boolean isMan = true;
  19. boolean isMan = false;
  20. if (isMan) {
  21. System.out.println("真男人");
  22. }else {
  23. System.out.println("女人");
  24. }
  25. }
  26. @Test
  27. public void testIfElseIfElse() {
  28. /* * 需求: * 级别:level=1 经理 * level=2 组长 * level=3 员工 */
  29. int level = 2;
  30. if (level == 1) {
  31. System.out.println("经理");
  32. } else if(level == 2) {
  33. System.out.println("组长");
  34. }else if (level == 3) {
  35. System.out.println("员工");
  36. }else {
  37. System.out.println("不是公司数据!");
  38. }
  39. }
  40. }
  • switch~case:
    了解即可,基本可以通过if-else来替代,而且java的switch很笨,远不如其他语言灵活,例如vb,cpp。

结构式:

  1. switch (key) {
  2. case value:
  3. break;
  4. default:
  5. break;
  6. }

测试用例:

  1. package cn.daniel.day03;
  2. import org.junit.Test;
  3. //多分支语句
  4. public class TestSwitch {
  5. @Test
  6. public void testSwitchCase() {
  7. int phone = 110;
  8. switch(phone) {
  9. case 110 :
  10. System.out.println("我是警察");
  11. break;
  12. case 120 :
  13. System.out.println("我是医生");
  14. break;
  15. case 119 :
  16. System.out.println("我是消防");
  17. break;
  18. default :
  19. System.out.println("错误号码!!!");
  20. break;
  21. }
  22. }
  23. }

控制台输出:我是警察
switch case 语句有如下规则:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

(二)循环结构

  • for循环
    结构式:

    for (初始化; 布尔表达式; 更新) {

    1. 语句块;
    2. }

当判断条件不成立时,结束for循环

演示案例:

  1. package cn.daniel.day03;
  2. public class TestFor {
  3. public static void main(String[] args) {
  4. //打印一个星星
  5. System.out.println("*");
  6. System.out.println();
  7. //控制台打印10个星星
  8. for (int i = 0; i < 10; i++) {
  9. System.out.println("*");
  10. }
  11. }
  12. }

双层循环

  1. package cn.daniel.day03;
  2. public class TestFor2 {
  3. public static void main(String[] args) {
  4. for (int i = 0; i < 5; i++) {
  5. for (int j = 0; j < 10; j++) {
  6. System.out.print("*");
  7. }
  8. System.out.println();
  9. }
  10. }
  11. }

for循环习惯使用小写字母作为变量:i,j,m,n,k
注意:for循环不能交叉,必须嵌套

三重循环

  1. for(int i=0; i<5; i++) {
  2. for(int j=0; j<5; j++) {
  3. for(int k=0; k<5; k++) {
  4. System.out.println("i="+i + ", j="+j + ",k="+k);
  5. }
  6. }
  7. }

问题:
内循环先执行完,还是外循环先执行完?
答:内循环

层数很多,程序可读性非常差,开发人员难理解,维护你的代码时晕了。
一般情况下java不超过3层

  • while循环
    while循环和do-while循环差异,while循环可能一次都不执行,而do-while循环至少执行一次
    (文件处理时需要最少先执行一次)

    package cn.tedu.hello.day03;

    public class TestWhile {

    1. public static void main(String[] args) {
    2. for(int i=0; i<10; i++) {
    3. System.out.print(i);
    4. }
    5. System.out.println();
    6. int i = 0; //初始化
    7. while( i<0 ) { //判断,true继续执行,false结束执行
    8. //循环体
    9. System.out.print(i);
    10. i++; //一定要改变i的值,不改变死循环
    11. }
    12. System.out.println();
    13. int j = 0;
    14. do {
    15. System.out.print(j);
    16. j++; //在循环体内一定要改变判断条件值
    17. }while( j<0 );
    18. }

    }

  • 退出循环






















关键字 作用
break break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。break 跳出最里层的循环,并且继续执行该循环下面的语句。
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。在 for 循环中,continue 语句使程序立即跳转到更新语句。在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
return 返回调用者

经典案例:

(一)九九乘法表

  1. package cn.daniel.day03;
  2. public class Test1 {
  3. public static void main(String[] args) {
  4. //9 * 9 乘法表
  5. for (int i = 0; i < 10; i++) { //控制层数
  6. for (int j = 0; j < i; j++) { //控制每层的输出
  7. System.out.print(i+"*"+j+"="+i*j+" ");
  8. }
  9. System.out.println();//换行
  10. }
  11. }
  12. }

控制台显示:
在这里插入图片描述
(二)打印菱形

  1. package cn.daniel.day03;
  2. public class TestPrint {
  3. public static void main(String[] args) {
  4. print(8);
  5. // print(9);
  6. }
  7. public static void print(int size) {
  8. //行数判断
  9. if (size%2 == 0) {
  10. size++;//计算菱形的大小
  11. }
  12. //上半部分
  13. for (int i = 0; i < size/2 + 1; i++) {
  14. for (int j = size/2 + 1; j > i + 1 ; j--) {
  15. //输出左上角位置的空白处
  16. System.out.print(" ");
  17. }
  18. for (int j = 0; j < 2 * i + 1; j++) {
  19. //打印上半部分的形状
  20. System.out.print("*");
  21. }
  22. System.out.println();//换行
  23. }
  24. //下半部分
  25. for (int i = size/2 + 1; i < size; i++) {
  26. for (int j = 0; j < i - size/2; j++) {
  27. //打印左下角空白格
  28. System.out.print(" ");
  29. }
  30. for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
  31. //输出菱形下半部分
  32. System.out.print("*");
  33. }
  34. System.out.println();
  35. }
  36. }
  37. }

控制台显示:
在这里插入图片描述
(三)镂空三角形

  1. package cn.daniel.day03;
  2. import java.util.Scanner;
  3. public class Test2 {
  4. public static void main(String[] args) {
  5. //输入层数
  6. Scanner scanner = new Scanner(System.in);
  7. System.out.println("请输入你要打印的层数:");
  8. int input = scanner.nextInt();//从键盘获取数据
  9. //控制层数
  10. for (int i = 0; i <= input; i++) {
  11. for (int j = input; j > i; j--) { //打印空格
  12. System.out.print(" ");
  13. }
  14. //控制每层的输出量
  15. for (int j2 = 1; j2 <= 2 * i + 1; j2++) {
  16. //第一层和最后一层都必须打印“ * ”
  17. if (i==0 || i==input) {
  18. System.out.print("*");
  19. }else if (j2==1 || j2==2*i+1) {
  20. System.out.print("*");
  21. }else {
  22. System.out.print(" ");//其他层数内部输出空格
  23. }
  24. }
  25. //换行
  26. System.out.println();
  27. }
  28. }
  29. }

控制台显示:
在这里插入图片描述
(四)镂空菱形

  1. package cn.daniel.day03;
  2. import java.util.Scanner;
  3. public class Test3 {
  4. public static void main(String[] args) {
  5. //输入层数
  6. Scanner scanner = new Scanner(System.in);
  7. System.out.println("请输入你要打印的层数:");
  8. int input = scanner.nextInt();//从键盘获取数据
  9. //控制层数
  10. for (int i = 0; i <= input; i++) {
  11. for (int j = input; j > i; j--) { //打印空格
  12. System.out.print(" ");
  13. }
  14. //控制每层的输出量
  15. for (int j2 = 1; j2 <= 2 * i + 1; j2++) {
  16. if (i==0) {
  17. System.out.print("*");
  18. }else if (j2==1 || j2==2*i+1) {
  19. System.out.print("*");
  20. }else {
  21. System.out.print(" ");//其他层数内部输出空格
  22. }
  23. }
  24. System.out.println();//换行
  25. }
  26. for (int i = input; i >= 0 ; i--) {
  27. for (int j = input - i; j >= 0; j--) {
  28. System.out.print(" ");
  29. }
  30. for (int j2 = 2*i-1; j2 >= 1; j2--) {
  31. if (i == 1) {
  32. System.out.print("*");
  33. }else if(j2 == 1 || j2 == 2*i-1){
  34. System.out.print("*");
  35. }else {
  36. System.out.print(" ");
  37. }
  38. }
  39. System.out.println();
  40. }
  41. }
  42. }

控制台显示:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,165人围观)

还没有评论,来说两句吧...

相关阅读