学习笔记——String字符串

一、重点内容

 

 

 

二、学习内容

统计一个字符串在另一个字符串中出现的次数
package com.jsoft.morning;



/*
    统计一个字符串在另一个字符串中出现的次数
 */
public class Ch05_Demo {

    public int sum(String target,String str){

        int count=0;
        int i=0;
        while (i<str.length()){
            int i1=str.indexOf(target,i);
            if (i1 != -1){
                count++;
                i=i1+1;
            }else {
                i++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Ch05_Demo ch05_demo = new Ch05_Demo();
        System.out.println( ch05_demo.sum("a","abcdefgabcdaaa"));

        //String s1="abcdefgabcdaaa";

        //a在s1中出现了多少次?
        /*//第一种
        char target = 'a';
        int count=0;
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i)==target){
                count++;
            }
        }
        System.out.println(target+"在"+s1+"中出现了"+count+"次");*/
        //第二种
        //char target = 'b';

        //System.out.println(target+"在"+s1+"中出现了"+count+"次");

    }


}
键盘输入身份证号,
输出性别,生日
package com.jsoft.morning;

import java.util.Scanner;


public class Ch06_Demo {

    public String getGener(String cardId){
        int genderNum = cardId.charAt(16);
        String gender = "男";
        if (genderNum % 2 ==0){
            gender = "女";
        }

        return gender;
    }
    public String getBirthday(String cardId){
        return cardId= cardId.substring(6,14);

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入身份证号");
        //数据校验
        String cardId = sc.next();
        Ch05_Demo ch05_demo = new Ch05_Demo();
        if (cardId.length()==18){
            System.out.println(cardId);
        }else {
            System.out.println("输入有误,请重新输入");
        }

    }
}

包装器

package com.jsoft.afternoon;

        Integer i1 = 20;
        Integer i2 = Integer.valueOf(50);
        int ii = i2;
        int ii2 = i2.intValue();
    }
}

自动装箱

package com.jsoft.afternoon;
/*
    数字格式化异常:NumberFormatException
 */
public class Ch04 {
    public static void main(String[] args) {
        String s = "1990";
        //自动装箱
        Integer i = Integer.parseInt(s);
        Integer.toString(i);
        System.out.println(2022-1990);
    }
}



注册和登录

package manager;

import java.util.Scanner;


public class Demo {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        UserManager userManager = new UserManager();
        EmployeeManager employeeManager = new EmployeeManager();


        main:for (;;){
            User[] users = userManager.getUsers();
            System.out.println("请选择功能:1、注册  2、登录 3、查询账户");
            String flag = sc.next();
            u:for(;;){
                switch (flag) {
                    case "1":
                        System.out.println("请输入账号:");
                        String username = sc.next();
                        for (User user : users) {
                            if(user != null){
                                if(username == user.getUsername()){
                                    System.out.println("用户名不能重复,请重新输入!");
                                    continue u;
                                }
                            }
                        }
                        System.out.println("请输入密码:");
                        String password = sc.next();
                        String info = userManager.register(username, password);
                        System.out.println(info);

                        continue main;
                    case "2":
                        System.out.println("请输入账号:");
                        String username1 = sc.next();
                        System.out.println("请输入密码:");
                        String password1 = sc.next();

                        boolean b = userManager.login(username1, password1);
                        if(b){
                            // 进入到员工管理
                            Employee[] employees = employeeManager.getEmployees();
                            System.out.println("登录成功!");
                            e:for(;;){
                                System.out.println("请选择功能:1.添加员工  2.查询员工  3.修改员工  4.删除员工");
                                String selection = sc.next();
                                switch (selection) {
                                    case "1":
                                        System.out.println("请输入员工姓名:");
                                        String name = sc.next();
                                        String s = employeeManager.addEmp(name);
                                        System.out.println(s);
                                        continue e;
                                    case "2":
                                        System.out.println("请选择功能:1.根据id查询  2.查询所有");
                                        String s2 = sc.next();
                                        switch (s2) {
                                            case "1":
                                                System.out.println("请输入id:");
                                                int id = sc.nextInt();
                                                Employee empById = employeeManager.getEmpById(id);
                                                if(empById != null){
                                                    System.out.println("你要找的" + id + "号员工信息为:" + empById.getName());
                                                }else {
                                                    System.out.println("你要找的" + id + "号员工不存在!");
                                                }
                                                continue e;
                                            case "2":
                                                for (Employee employee : employees) {
                                                    if(employee != null){
                                                        System.out.println(employee.getId() + "," + employee.getName());
                                                    }
                                                }
                                                continue e;
                                        }
                                    case "3":
                                        // 修改之前要先查询
                                        System.out.println("请输入工号:");
                                        int id = sc.nextInt();
                                        Employee emp = employeeManager.getEmpById(id);
                                        if(emp != null){
                                            System.out.println("你要修改的" + id + "号员工信息为:" + emp.getName());
                                            System.out.println("请输入新的姓名:");
                                            String name1 = sc.next();
                                            emp.setName(name1);
                                            // 需要把当前已经修改好的对象重新给数组
                                            for (int i = 0; i < employees.length; i++) {
                                                if(employees[i] == emp){
                                                    employees[i] = emp;
                                                }
                                            }
                                            System.out.println("修改成功!新的信息为:" + emp.getId() + "," + emp.getName());
                                        }else {
                                            System.out.println("你要找的" + id + "号员工不存在!");
                                        }
                                    case "4":
                                        System.out.println("请输入工号:");
                                        int id1 = sc.nextInt();
                                        Employee emp1 = employeeManager.getEmpById(id1);
                                        if(emp1 != null){
                                            for (int i = 0; i < employees.length; i++) {
                                                if(employees[i] == emp1){
                                                    employees[i] = null;
                                                }
                                            }
                                            // 移位
                                            System.out.println("删除成功!");
                                        }else {
                                            System.out.println("你要找的" + id1 + "号员工不存在!");
                                        }
                                }
                                continue e;
                            }
                        }else {
                            continue main;
                        }

                }
            }
        }
    }
}
package manager;

public class Employee {

    private int id;
    private String name;

    public Employee() {
    }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package manager;


public class EmployeeManager {

    private Employee [] employees = new Employee[10];
    private int index = 0;

    private int id = 1001;

    public Employee getEmpById(int id){
        Employee emp = null;
        for (Employee employee : employees) {
            if(employee != null){
                if(id == employee.getId()) {
                    emp = employee;
                }
            }
        }
        return emp;
    }

    public String addEmp(String name){
        Employee employee = new Employee(id,name);
        employees[index] = employee;

        index ++;


        return "添加成功,工号:" + (id++) + ",姓名:" + name;
    }

    public Employee[] getEmployees() {
        return employees;
    }

    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }
}
package manager;

public class User {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}
package manager;

public class UserManager {

    // User类型的数组
    // User类型的数中保存的就是一个一个的User对象
    private User [] users = new User[10];
    private int index = 1;

    public UserManager() {
        users[0] = new User("admin","123456");
    }

    public String register(String username, String password){
        // 保存账号密码,保存的是User对象
        // 构建User对象
        User user = new User(username,password);
        users[index] = user;

        index ++;
//        this.index = this.index + 1;

        return "注册成功,账号:" + username + ",密码:" + password;
    }

    public boolean login(String username,String password) {

        boolean b = false;
        for (User u : users) {
            if(u != null){
                if(u.getUsername().equals(username) && u.getPassword().equals(password)) {
                    b = true;
                    break;
                }
            }
        }
        return b;

    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }


}

 

 

、笔记内容

 

posted @ 2022-07-20 22:02  LJMMJL  阅读(31)  评论(0)    收藏  举报