ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

java基础流程控制笔记

2026/8/9 0:34:32 拓冰建站 浏览量
java基础流程控制笔记

Java流程控制

用户交互Scanner

import java.util.Scanner类获取用户信息,提供与用户交互的方式

Scanner scanner = new Scanner(System.in);

通过next() 到空白会截断 nextLine()到回车结束 获取输入字符串

Scanner str = scanner.nextLine();

用hasNext() hasNextLine()判断

IO流会占用资源,最后scanner.close()

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入三位学生成绩,用空格隔开:");int score1 = scanner.nextInt();int score2= scanner.nextInt();int score3 = scanner.nextInt();int avg = (score1 + score2 + score3) / 3;System.out.println(avg);}}

选择结构

  1. 基本if选择结构 if(){}

  2. if else选择结构

  3. 三元一次选择结构? :

  4. 嵌套if选择结构

    public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入是否获胜(Y/N):");String win =sc,next;if ("Y",equils(win)){System.out.println("请输入性别:");      if("男".equils(sex)){System.out.println("进入男子组决赛")}else {System.out.println("进入女子组决赛");}}else{System.out.println("已被淘汰");}}
    }
    

    ** 注: **逻辑与和或短路: && ||

    1. switch选择结构 :支持byte short int Enum String(JDK7后支持)

    switch(表达式){

    ​ case 常量1:

    ​ break;

    }

    转换思想:(1|2|3 -1 )/3 = 0 (4| 5|6 -1)/3 = 1

    总结

    **1. 选择结构 **

    基本if if-else 嵌套if 多重if switch

    2.switch选择结构多重if选择结构的异同

    相同:

    都可以来处理多分支结构

    不同:

    switch只用于可穷举情况,多重if选择结构适用于选择结构的所有场景,但多重if选择结构还支持对区间的描述

    Scanner输入验证

    //校验Scanner储存结构的数据中是否有下一个整数

    //则让用户输入

    if(sc.hasNextInt()){

    }else

    程序调试

    1. 什么是程序调试

      程序出现问题时希望程序能够暂停下来,操作代码逐行执行,观察程序中变量的变化是否按照我们设计程序的思维变化,从而找问题并解决

    2. 为什么需要程序调试

      人无完人,要定位问题

    3. 如何进行程序调试

      在有效的代码上下断点

      然后以DEBUG模式启动程序

      程序启动后,通过操作使程序代码逐行执行,观察变量的值的变化,从而找出问题并解决

循环结构

1.什么是循环

同样的事情反复做多次

  1. 为什么用

    去除冗余部分(重复的编码)

  2. 循环三要素

    定义循环变量并赋初值

    循环条件

    循环变量的更新

    1. while循环

      while(循环条件){}
      

      先判断后执行

      水仙花数

      public class Main {public static void main(String[] args) {int number =100;while (number < 1000) {int one = number % 10;int two = number / 10 %10;int three = number / 100;if (one*one*one + two*two*two +three*three*three == number) {System.out.println(number);}number++;}}
      }
      
      1. do-while 循环

        ed:记录总成绩

        public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int totalSore =0;int score;do {System.out.print("Enter your score: ");score = scanner.nextInt();totalSore += score;}while (score !=0);System.out.println("Total Sore: " + totalSore);}
        }
        

        至少执行一次

      2. for循环

        for(定义变量并赋初值;循环条件;循环变量的更新)

      3. 总结

        循环数确定选for

break&continue

break只能应用于while do-while for switch

continue只能应用于循环结构while do-while for

多重循环

ed:

  1. 99乘法表

  2. 重复图形

    分析实现

    善用用||控制图形打印

  3. 求质数

  4. 学生管理系统

    public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (true) {System.out.println("====================");System.out.println("1.学生成绩管理");System.out.println("2.学生选课管理");System.out.println("3.退出系统");System.out.println("====================");System.out.println("请选择菜单编号");int menuNo = scanner.nextInt();if (menuNo == 1) {chlidMenu :while(true){System.out.println("************************");System.out.println("1.添加成绩");System.out.println("2.查看成绩");System.out.println("3.修改成绩");System.out.println("4.删除成绩");System.out.println("5.返回主菜单");System.out.println("************************");System.out.println("请选择菜单编号:");int option = scanner.nextInt();switch (option) {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 chlidMenu;}}} else if (menuNo == 2) {} else {System.out.println("感谢使用本人开发的系统");break;}}}
    }