当前位置: 首页 > article >正文

Java 基础入门代码示例解析

在 Java 编程的学习过程中,理解函数(方法)的使用以及简单系统功能的实现是非常重要的基础。本文将对一系列 Java 代码进行详细解析,这些代码涵盖了菜单驱动的功能选择、数据查询以及简单的 RBAC(基于角色的访问控制)系统模拟等内容。

目录

一、三国群英系统功能实现

二、数字操作功能实现

三、人物信息查询功能实现

四、RBAC 系统模拟实现


因为上篇全是RBAC系统&Java的概念,太空洞,所以补充了这篇关于RBAC系统相关的代码示例,希望可以帮助大家对其理解有着更好的印象。如果想知道上篇内容请点链接

Java基础入门与RBAC系统设计:从方法到面向对象编程-CSDN博客

一、三国群英系统功能实现

  1. 菜单展示函数
​
public static void  menu(){
    System.out.println("欢迎大家进入三国群英系统");
    System.out.println("按1.查询所有英雄");
    System.out.println("按2.根据姓氏查询英雄");
    System.out.println("按3.根据关键字查询英雄");
}

​

menu 函数用于在控制台输出 “三国群英系统” 的操作菜单,为用户提供系统功能的选择提示。

  1. 主函数及功能选择逻辑

在 main 函数中,首先创建了 Scanner 对象用于读取用户输入,并定义了一个包含英雄名字的字符串数组 arrs。通过一个无限循环 while (true) 不断展示菜单,获取用户输入的选项,并使用 switch 语句根据选项调用相应的功能函数。如果用户输入的选项不在有效范围内(123),则输出错误提示信息。

  1. 英雄查询功能函数
​
public static void findByKeyWord(String[] arrs,Scanner scanner){
    System.out.println("请输入关键字:");
    String keyword = scanner.next();
    for (String hero : arrs) {
        if (hero.contains(keyword)) {
            System.out.println(hero);
        }
    }
}

public  static void findByPrefix(String[] arrs,Scanner scanner){
    System.out.println("请输入姓氏:");
    String name = scanner.next();
    for (String hero : arrs) {
        if (hero.startsWith(name)) {
            System.out.println(hero);
        }
    }
}

public  static void findAll(String[] arrs  ){
    // 查询所有英雄,直接遍历输出即可
    //增强for循环
                for(String hero:arrs){
                    System.out.println(hero);
                }
}

​

findByKeyWord 函数接受用户输入的关键字,遍历英雄数组,输出包含该关键字的英雄名字。findByPrefix 函数接受用户输入的姓氏,遍历数组,输出以该姓氏开头的英雄名字。findAll 函数则简单地遍历英雄数组,输出所有英雄的名字。

二、数字操作功能实现

  1. 菜单展示函数
​
public static void showMenu(){
    System.out.println("按1.打印所有的数字");
    System.out.println("按2.打印所有的负数");
    System.out.println("按3.输入下标打印对应位置的数字");
    System.out.println("按4.输入数字打印对应数字所在的下标");
}

​

showMenu 函数用于展示数字操作的菜单选项,提示用户可以进行的操作。

  1. 主函数及功能选择逻辑
​
public static void main(String[] args) {
    int[] arrs = {11, 23, -22, 12, 43, 34};
    while (true) {
        showMenu();//显示菜单
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                findAll(arrs); //按1.打印所有的数字
                break;
            case 2:
                findByZero(arrs);//按2.打印所有的负数
                break;
           case 3:
                findBySub(arrs, scanner);//按3.输入数字打印对应数字所在的下标
                break;
            case 4:
                findByNum(arrs, scanner);//按3.输入数字打印对应数字所在的下标
                break;
        }
    }
}

​

在 main 函数中,定义了一个包含数字的整数数组 arrs。通过无限循环展示菜单,获取用户输入的选项,并根据选项调用相应的功能函数。

  1. 数字操作功能函数
​
public static void findAll(int[] arrs){
    for(int num:arrs){
        System.out.println(num);
    }
}

public static void findByZero(int[] arrs) {
    for (int i = 0; i < arrs.length; i++) {
        if (arrs[i] < 0) {
            System.out.println(arrs[i]);
        }
    }
}

public static void findBySub (int[] arrs, Scanner scanner){
        System.out.print("请输入下标:");
        int index = scanner.nextInt();
        if (index >= 0 && index < arrs.length) {
            System.out.println("下标 " + index + " 对应的数字是: " + arrs[index]);
        } else {
            System.out.println("下标超出范围");
        }
    }

public static void findByNum(int[] arrs, Scanner scanner){
        findAll(arrs);
        System.out.print("查询下标,输入数字:");
        int num =scanner.nextInt();
    for (int i = 0; i < arrs.length; i++) {
        if (arrs[i]==num){
            System.out.println("下标是"+i);
            break;
        }
}
}

​

findAll 函数遍历数组并输出所有数字。findByZero 函数遍历数组,输出所有负数。findBySub 函数接受用户输入的下标,检查下标是否在有效范围内,如果有效则输出对应位置的数字,否则输出错误提示。findByNum 函数先输出所有数字,然后接受用户输入的数字,遍历数组查找该数字的下标并输出。

三、人物信息查询功能实现

  1. 菜单展示函数
​
public static void showMenu() {
    System.out.println("按1.查询所有的人物");
    System.out.println("按2.根据类型查询对应的人物");
    System.out.println("按3.根据年龄查询对应的人物");
}

​

showMenu 函数展示人物信息查询系统的菜单选项。

  1. 主函数及功能选择逻辑
​
public static void main(String[] args) {
    String[] arrs = {"黄忠-武将-55", "貂蝉-美女-20", "赵云-武将-30", "张武将-美女-20", "许褚-武将-35", "吕布-武将-33"};
    Scanner scanner = new Scanner(System.in); // Scanner outside the loop

    while (true) {
        showMenu();// 显示菜单
        int i = scanner.nextInt();
        scanner.nextLine(); // Consume newline character

        switch (i) {
            case 1:
                findAll(arrs);// 按1.查询所有的人物
                break;
            case 2:
                findEx(arrs, scanner);// 根据类型查询对应的人物
                break;
            case 3:
                findByAge(arrs, scanner);
                break;
            default:
                System.out.println("无效的选项,请重新输入。");
        }
    }
}

​

在 main 函数中,定义了一个包含人物信息(姓名 - 类型 - 年龄)的字符串数组 arrs。通过无限循环展示菜单,获取用户输入的选项,并根据选项调用相应的功能函数。注意在获取用户输入选项后,使用 scanner.nextLine() 来清除输入缓冲区中的换行符,避免影响后续输入。

  1. 人物信息查询功能函数
​
public static void findAll(String[] arrs) {
    for (String arr : arrs) { // Iterate through the array and print each element
        System.out.println(arr);
    }
}

public static void findEx(String[] arrs, Scanner scanner) {
    System.out.println("查询的类型是其中的?(武将,美女)");
    String example = scanner.next();
    scanner.nextLine(); // Consume newline
    for (String arr : arrs) {
        String[] parts = arr.split("-");
        if (parts.length == 3 && parts[1].equals(example)) {
            System.out.println(arr);
        }
    }
}

public static void findByAge(String[] arrs, Scanner scanner) {
    System.out.println("查询的年龄是?");
    int age = scanner.nextInt();
    scanner.nextLine(); // Consume newline

    for (String arr : arrs) {
        String[] parts = arr.split("-");
        if (parts.length == 3) {
            try {
                int personAge = Integer.parseInt(parts[2]);
                if (personAge == age) {
                    System.out.println(arr);
                }
            } catch (NumberFormatException e) {
                System.out.println("年龄格式不正确: " + arr); // Handle potential errors
            }
        }
    }
}

​

findAll 函数遍历数组输出所有人物信息。findEx 函数接受用户输入的类型,遍历数组,根据人物信息的类型进行匹配并输出相应的人物信息。findByAge 函数接受用户输入的年龄,遍历数组,将人物信息中的年龄解析为整数并与用户输入的年龄进行匹配,输出符合条件的人物信息,同时处理年龄格式不正确的情况。

四、RBAC 系统模拟实现

  1. 菜单展示函数
​
public static void showMenu() {
    System.out.println("--------------------欢迎进入RBAC系统--------------------");
    System.out.println("按1.查询所有部门");
    System.out.println("按2.根据ID查询部门");
    System.out.println("按3.根据名称查询部门");
}

​

showMenu 函数展示 RBAC 系统的菜单选项。

  1. 主函数及功能选择逻辑
​
public static void main(String[] args) {
    String[] depts = {"1-生活部", "2-宣传部", "3-礼仪部", "4-财务部"};
    Scanner scanner = new Scanner(System.in); // Scanner outside the loop

    for (; ; ) {
        showMenu();
        int i = scanner.nextInt();
        scanner.nextLine();

        switch (i) {
            case 1:
                findAll(depts);
                break;
            case 2:
                findById(depts, scanner);
                break;
            case 3:
                findByName(depts, scanner);
                break;
            default:
                System.out.println("无效的选项,请重新输入。");
        }
    }
}

​

在 main 函数中,定义了一个包含部门信息(ID - 部门名称)的字符串数组 depts。通过无限循环展示菜单,获取用户输入的选项,并根据选项调用相应的功能函数。

  1. 部门信息查询功能函数
​
public static void findAll(String[] arrs) {
    for (String arr : arrs) {
        System.out.println(arr); //直接输出
    }
}

public static void findById(String[] arrs, Scanner scanner) {
    System.out.println("输入要查询的Id号码");
    int idNum = scanner.nextInt();
    scanner.nextLine(); // Consume newline

    boolean found = false;
    for (String arr : arrs) {
        String[] parts = arr.split("-");
        if (parts.length == 2) {
            try {
                int deptId = Integer.parseInt(parts[0]);
                if (deptId == idNum) {
                    System.out.println("找到部门: " + arr);
                    found = true;
                    break; // Exit loop after finding the department
                }
            } catch (NumberFormatException e) {
                System.out.println("部门ID格式不正确: " + arr);
            }
        }
    }
    if (!found) {
        System.out.println("未找到ID为 " + idNum + " 的部门");
    }
}

public static void findByName(String[] depts, Scanner scanner) {
    System.out.println("输入要查询的部门名称");
    String deptName = scanner.nextLine();

    boolean found = false;
    for (String dept : depts) {
        String[] parts = dept.split("-");
        if (parts.length == 2 && parts[1].equals(deptName)) {
            System.out.println("找到部门: " + dept);
            found = true;
            break;
        }
    }
    if (!found) {
        System.out.println("未找到名称为 " + deptName + " 的部门");
    }
}

​

findAll 函数遍历数组输出所有部门信息。findById 函数接受用户输入的部门 ID,遍历数组,将部门信息中的 ID 解析为整数并与用户输入的 ID 进行匹配,输出符合条件的部门信息,同时处理 ID 格式不正确的情况以及未找到对应部门的情况。findByName 函数接受用户输入的部门名称,遍历数组,根据部门名称进行匹配并输出相应的部门信息,处理未找到对应部门的情况。

通过以上代码示例,我们可以看到如何使用 Java 函数(方法)来实现不同系统的功能选择和数据查询操作,这对于理解 Java 编程的基本概念和逻辑非常有帮助。希望读者能够通过这些示例更好地掌握 Java 编程的基础知识。


http://www.kler.cn/a/600026.html

相关文章:

  • idea底部图标不小心关了,怎么重新打开?
  • Leetcode—15. 三数之和(哈希表—基础算法)
  • Java多线程与高并发专题——使用 Future 有哪些注意点?Future 产生新的线程了吗?
  • 内网渗透-隧道通信
  • Python技术栈与数据可视化创意实践详解(三)
  • 【进阶编程】跨平台的 UI 框架
  • JVM 02
  • STM32G030移植RT-Thread
  • 学一个前端 UI 框架,要学些什么内容?
  • 当人类关系重构:从“相互需要”到“鹅卵石化”——生成式人工智能(GAI)认证的角色与影响
  • 探索AI的无限可能,体验智能对话的未来,大模型 API 演示
  • linux ptrace 图文详解(三) PTRACE_ATTACH 跟踪程序
  • Edge浏览器如何默认启动某个工作区 / 为工作区添加快捷方式
  • docker 容器 php环境中安装gd 、mysql 等扩展
  • 数据库原理及应用mysql版陈业斌实验一
  • 【Docker系列二】 Docker 镜像
  • Spring-Mybatis框架常见面试题
  • Java面试第十三山!《设计模式》
  • 快速部署Samba共享服务器作为k8s后端存储
  • Android adb调试应用程序