首页 > 其他分享 >第三周学习日历

第三周学习日历

时间:2024-07-20 19:31:48浏览次数:12  
标签:String int 日历 第三周 System 学习 println public out

这一周在练车,感觉练学车好难...
在空余时间我也学习了Java的一些知识,在学习中自己敲了一些代码记录下来...
这周最大的进步是自己根据要求写了一个学生管理系统,虽然之前用C++写过类似的,但是用一门新的语言,有不同的成就感>_<
7月15日参加了睿康的编程赛 写出来3题半 1000多名 不知道能不能拿奖 感觉自己花200块白坐了俩小时
算法与数据结构这周没学习,感觉自己第一周定的计划也泡汤了 哈哈 没事
键盘录入

1.
//nextInt() 接收整数
//nextDouble() 接收小数
//next() 接收字符串--遇到空格,制表符,回车就停止接收
2.
//nextLine();接受字符串--遇到回车才会停止 
//
package Car;

public class Car {
    private String brand;
    private int price;
    private String color;

    //默认构造
    public Car() {
    }

    //有参构造
    public Car(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void showCarInfo(){
        System.out.println(this.brand + ' ' + this.color + ' ' + this.price);
    }
}

package Car;

import java.util.Scanner;

public class CarTest {
    public static void main(String[] args){
        Car[] arr = new Car[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            Car c = new Car();
            System.out.println("请输入车的品牌");
            String brand = sc.next();
            c.setBrand(brand);
            System.out.println("请输入车的颜色");
            String color = sc.next();
            c.setColor(color);
            System.out.println("请输入车的价格");
            int price = sc.nextInt();
            c.setPrice(price);
            arr[i] = c;
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i].showCarInfo();
        }
    }
}

package Stu;

public class Student {
    private int id;
    private String name;
    private int age;


    public Student() {
    }

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

    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;
    }


    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        this.age = age;
    }

}

package Stu;

public class StudentTest {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student stu1 = new Student(1001,"张三",20);
        Student stu2 = new Student(1002,"李四",19);

        arr[0] = stu1;
        arr[1] = stu2;
        Student stu3 = new Student(1003,"王五",21);
        boolean flag = contains(arr,stu3);
        if(flag){
            System.out.println("学生编号重复!");
        }
        else{
            int count = countArr(arr);
            if(count < arr.length){
                arr[count] = stu3;
            }
            else {
                Student[] newArr = creatNewArr(arr);
                newArr[count] = stu3;
                arr = newArr;
            }
        }
        printArr(arr);

    }

    public static int countArr(Student[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null)count++;
            else break;
        }
        return count;
    }

    public static boolean contains(Student[] arr,Student stu){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                if(arr[i].getId() == stu.getId()) return true;
            }
        }
        return false;
    }

    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                System.out.println(arr[i].getId() + "  " + arr[i].getName() + "  "+ arr[i].getAge());
            }
        }
    }

    public static Student[] creatNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

}


API(Application programming Interface):应用程序编程接口
简单理解就是别人以及写好的东西,直接用
Java API:指的就是JDK中提供的各种功能的Java类
字符串不可变,他们的值在创建后不能被更改

package StringTest;

//String 的构造函数
public class Str {
    public static void main(String[] args) {

        //1.直接赋值
        String s1 = "abc";
        System.out.println(s1);
        //2.new关键字
        String s2 = new String("abcd");
        System.out.println(s2);
        //3.根据字符数组,创建字符串对象
        char[] chs = {'a','b','c','d','e'};
        String s3 = new String(chs);
        System.out.println(s3);
        //4.根据字节数组,创建字符串对象
        byte[] bytes = {97,98,99,100,101,102};
        String s4 = new String(bytes);
        System.out.println(s4);

    }
}

字符串当使用树干引号直接赋值时,系统会检查该字符串在串池中是否存在
不存在:创建新的
存在:复用
String直接赋值会比较节省空间,

基本数据类型比较的是数据值,引用数据类型比较的是地址值

字符串比较

  • boolean equals方法(要比较的字符串) 完全一样为true,否则为false
  • boolean equalsIgnoreCase(要比较的字符串) 忽略大小写
package StringTest;

public class Equal {
    public static void main(String[] args) {

        String s1 = "abc";
        String s2 = new String("abc");
        System.out.println(s1 == s2);

        System.out.println(s1.equals(s2));

        System.out.println(s1.equalsIgnoreCase(s2));
    }

}
**键盘录入的字符串是new出来的**
**遍历字符串**
* public char charAt(int index); 根据索引返回字符
* public int length(); 返回字符串的长度
* 字符串的长度 : 字符串对象.length() 
package StringTest;

import java.util.Scanner;

public class StringDemo {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        String s1 = sc.next();
        for (int i = 0; i < s1.length(); i++) {
            //字符串对象.charAt(index) 取出字符
            System.out.println( s1.charAt(i) );
        }
    }
}
//字符串拼接
package StringTest;

public class StringDemo2 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        String res = arrToString(arr);
        System.out.println(res);
    }

    public static String arrToString(int[] arr){
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                result += arr[i] + "]";
            }
            else result += arr[i] + ", ";
        }
        return result;
    }

}

package StringTest;

import java.util.Scanner;

//字符串反转
public class StringDemo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String result = "";
        for(int i = s.length() - 1; i >= 0; i--){
            result += s.charAt(i);
        }
        System.out.println(result);
    }
}


package me.JavaStudy;

import java.util.Scanner;
//币值转换
public class Captial {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字");
        int number = sc.nextInt();
        String result = "";
        String[] arr = {"佰","什","万","仟","佰","什","元"};
        while(number != 0){
            result = captialNumber(number%10) + result;
            number /= 10;
        }
        int count = result.length();
        for(int i = count; i < 7; i++){
            result = "零" + result;
        }
        String ans = "";
        for (int i = 0; i < result.length(); i++) {
            ans += result.charAt(i) + arr[i];
        }
        System.out.println(ans);
    }

    public static String captialNumber(int num){
        String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        return arr[num];
    }

}

package me.JavaStudy;

public class StringDemo1 {
    public static void main(String[] args) {
        String s = "16834331202";
        String start = s.substring(0,3);
        String end = s.substring(7);
        String result = start + "****" + end;
        System.out.println(result);
    }
}

package me.JavaStudy;

//replace函数
public class StringDemo2 {
    public static void main(String[] args) {
        String s = "你好,我是张三";
        System.out.println(s);
        s = s.replace("张三","李四");
        System.out.println(s);
    }
}

StringBulider概述

StringBuilder 可以看成一个容器,创建之后里面的内容是可变的

  • 作用: 提高字符串的操作效率

使用StringBuilder的两个场景:1.字符串拼接 2.字符串反转
StringBuilder的使用案例

package me.JavaStudy;

public class StringBuliderDemo1 {
    public static void main(String[] args) {
        //1.创建对象  因为StringBuilder是Java已经写好的类,打印对象是属性值,而不是地址值
        StringBuilder sb = new StringBuilder("abcd");

        //2.添加元素
        sb.append(8.8888);

        int len = sb.length();
        System.out.println(len);


        //反转操作
        sb.reverse();
        System.out.println(sb);

        //添加字符串
        sb.append("abcdefg");

        //再把StringBuilder变回字符串
        String s = sb.toString();
        System.out.println(s);
    }
}

回文字符串

package me.JavaStudy;

import java.util.Scanner;

public class StringBuliderDemo1 {
    public static void main(String[] args) {

        //链式编程
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s =  sc.next();
        String result = new StringBuilder().append(s).reverse().toString();
        if(s.equals(result)){
            System.out.println("YES");
        }
        else System.out.println("NO");
    }
}

数组变成字符串

package me.JavaStudy;

public class StringBuliderDemo1 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        System.out.println(arrToString(arr));
    }

    public static String arrToString(int[] arr){
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1)sb.append(arr[i]).append("]");
            else sb.append(arr[i]).append(", ");
        }
        return sb.toString();
    }
}

StringJoiner概述

  • StringJoiner跟StringBuilder一样,也可以看成一个容器,创建之后里面的内容可以改变
  • 作用: 提高字符串的操作效率,而且代码编写特别简洁,但是市面上很少有人用(JDK8出现的)


StringJoiner没有空参构造

package me.JavaStudy;

import java.util.StringJoiner;

public class StringJoinerStudy {
    public static void main(String[] args) {
        StringJoiner sj = new StringJoiner("---");
        sj.add("aaa").add("bbb").add("ccc");
        System.out.println(sj);

        StringJoiner sj2 = new StringJoiner(",","[","]");
        sj2.add("1").add("2").add("3");
        String s = sj2.toString();
        System.out.println(s);
    }
}

调整字符串内容

package me.JavaStudy;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        int len = a.length();
        boolean flag = check(a,b);
        if(flag) System.out.println("YES");
        else System.out.println("NO");
    }
    
    public static boolean check(String a,String b){
        for (int i = 0; i < a.length(); i++) {
            a = rotate(a);
            if(a.equals(b))return true;
        }
        return false;
    }
    
    
    public static String rotate(String s){
        return  s.substring(1) + s.charAt(0);
    }
}


字符串相乘(非高精度)

package me.JavaStudy;

//字符串相乘(非高精度)
public class Test2 {
    public static void main(String[] args) {
        String s1 = "23456";
        String s2 = "1111";

        int a = 0 , b = 0;
        for (int i = 0; i < s1.length(); i++) {
            a = a * 10 + (s1.charAt(i) - '0');
        }
        for (int i = 0; i < s2.length(); i++) {
            b = b * 10 + (s2.charAt(i) - '0');
        }
        System.out.println(a*b);
    }

}

计算一句话中最后一个单词的长度

package me.JavaStudy;

import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int count = 0;
        for(int i = s.length() - 1; i >= 0; i--){
            if(s.charAt(i) == ' ')break;
            count++;
        }
        System.out.println(count);
    }
}

集合

集合和数组的对比:

  • 长度:集合可以自由伸缩
  • 存储类型: 数组都可以存,集合可以存引用数据类型,存基本数据类型需要变成包装类

ArrayList成员方法

package me.JavaStudy;

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args){
        //1.创建ArrayList
        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);
        //2.增删改查
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        System.out.println(list);
        System.out.println("集合大小" + list.size());

        list.remove("aaa");
        System.out.println(list);
        String s = list.remove(0);
        System.out.println(s);
        System.out.println(list);

        list.set(0,"0.0");
        System.out.println(list);

        for (int i = 0; i < list.size(); i++) {
            String s2 = list.get(i);
            System.out.println(s2);
        }
    }
}

包装类
int -> Integer
char -> Character
其他数据类型都是首字母大写变成包装类

package me.JavaStudy;

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args){
        //1.创建ArrayList
        ArrayList<Integer> list1 = new ArrayList<>();

        list1.add(1);
        list1.add(2);
        System.out.println(list1);

        ArrayList<Character>list2 = new ArrayList<>();
        list2.add('a');
        list2.add('b');
        System.out.println(list2);

        ArrayList<Double> list3 = new ArrayList<>();
        list3.add(1.00213);
        list3.add(1.0001212);
        System.out.println(list3);
    }
}

ArrayList接收自定义数据类型

package Test;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListTest {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 2; i++) {
            Student s = new Student();
            System.out.println("请输入学生姓名");
            String name = sc.next();
            System.out.println("请输入学生年龄");
            int age = sc.nextInt();
            s.setAge(age);
            s.setName(name);
            list.add(s);
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).showStudentInfo();
        }
    }
}

获取价格低于3000的手机

package Test;

import java.util.ArrayList;

public class ArrayListTest2 {
    public static void main(String[] args) {
        ArrayList<Phone> list = new ArrayList<>();
        Phone p1 = new Phone("小米", 2999);
        Phone p2 = new Phone("锤子",999);
        Phone p3 = new Phone("苹果",8999);

        list.add(p1);
        list.add(p2);
        list.add(p3);

        ArrayList<Phone> p = getPhoneInfo(list);
        for (int i = 0; i < p.size(); i++) {
            p.get(i).showPhoneInfo();
        }
    }

    public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list){
        ArrayList<Phone> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getPrice() < 3000){
                result.add(list.get(i));
            }
        }
        return result;
    }
}

学生管理系统简易版

package StudentSystemTest;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while(true){
            showMenu();
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> removeStudent(list);
                case "3" -> updateStudent( list);
                case "4" -> showStudentInfo(list);
                case "5" -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("输入有误,请重新输入");
            }
        }
    }

    //显示界面
    public static void showMenu() {
        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("请输入你的选择");
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("需要添加学生的数量");
        int count = sc.nextInt();
        for (int i = 0; i < count; i++) {
            System.out.println("输入学生id");
            String id = sc.next();
            System.out.println("输入学生姓名:");
            String name = sc.next();
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("输入学生家庭住址:");
            String address = sc.next();
            Student s = new Student(id,name,age,address);
            list.add(s);
        }
        System.out.println("添加成功!");
    }

    //删除学生
    public static void removeStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的学生id:");
        String id = sc.next();
        boolean flag = idIsExist(list,id);
        if(flag){
            int index = getIndex(list,id);
            list.remove(index);
            System.out.println("删除成功!");
        }
        else System.out.println("删除失败!,id不存在!");
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要修改的学生id");
        String id = sc.next();
        int index = getIndex(list,id);
        if(index > -1){
            System.out.println("输入学生id");
            String sid = sc.next();
            list.get(index).setId(sid);
            System.out.println("输入学生姓名:");
            String name = sc.next();
            list.get(index).setName(name);
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            list.get(index).setAge(age);
            System.out.println("输入学生家庭住址:");
            String address = sc.next();
            list.get(index).setAddress(address);
            System.out.println("修改成功!");
        }
        else System.out.println("修改失败!,id不存在!");
    }

    //查询学生
    public static void showStudentInfo(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).showStudentInfo();
        }
    }

    //判断id是否存在
    public static boolean idIsExist(ArrayList<Student> list,String id){
        int index = getIndex(list,id);
        if(index > -1)return true;
        return false;
    }

    //返回id对应的索引
    public static int getIndex(ArrayList<Student> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

}


学生管理系统升级版

package StudentSystemTest;

public class Student {
    private String id;
    private String name;
    private int age;
    private String address;

    public Student() {

    }

    public Student(String id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void showStudentInfo(){
        System.out.println(this.getId()+ " " + this.getName() + " "+this.getAge() + " " + this.getAddress());
    }

    /**
     * 获取
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

}

package StudentSystemTest;

public class User {
    private String username;
    private String password;
    private String idCard;
    private String phoneNumber;

    public User() {
    }

    public User(String username, String password, String idCard, String phoneNumber) {
        this.username = username;
        this.password = password;
        this.idCard = idCard;
        this.phoneNumber = phoneNumber;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 获取
     * @return idCard
     */
    public String getIdCard() {
        return idCard;
    }

    /**
     * 设置
     * @param idCard
     */
    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    /**
     * 获取
     * @return phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * 设置
     * @param phoneNumber
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}


package StudentSystemTest;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> userList = new ArrayList<>();
        loop : while(true){
            showLogin();
            String choose1 = sc.next();
            switch(choose1){
                case "1" ->
                {
                    boolean is = login(userList);
                    if(is) break loop;
                }
                case "2" -> register(userList);
                case "3" -> setPassword(userList);
                default -> System.out.println("输入有误,请重新输入!");
            }
        }
        ArrayList<Student> list = new ArrayList<>();
        while(true){
            showMenu();
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> removeStudent(list);
                case "3" -> updateStudent( list);
                case "4" -> showStudentInfo(list);
                case "5" -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("输入有误,请重新输入");
            }
        }
    }

    //显示界面
    public static void showMenu() {
        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("请输入你的选择");
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("需要添加学生的数量");
        int count = sc.nextInt();
        for (int i = 0; i < count; i++) {
            System.out.println("输入学生id");
            String id = sc.next();
            System.out.println("输入学生姓名:");
            String name = sc.next();
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("输入学生家庭住址:");
            String address = sc.next();
            Student s = new Student(id,name,age,address);
            list.add(s);
        }
        System.out.println("添加成功!");
    }

    //删除学生
    public static void removeStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的学生id:");
        String id = sc.next();
        boolean flag = idIsExist(list,id);
        if(flag){
            int index = getIndex(list,id);
            list.remove(index);
            System.out.println("删除成功!");
        }
        else System.out.println("删除失败!,id不存在!");
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要修改的学生id");
        String id = sc.next();
        int index = getIndex(list,id);
        if(index > -1){
            System.out.println("输入学生id");
            String sid = sc.next();
            list.get(index).setId(sid);
            System.out.println("输入学生姓名:");
            String name = sc.next();
            list.get(index).setName(name);
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            list.get(index).setAge(age);
            System.out.println("输入学生家庭住址:");
            String address = sc.next();
            list.get(index).setAddress(address);
            System.out.println("修改成功!");
        }
        else System.out.println("修改失败!,id不存在!");
    }

    //查询学生
    public static void showStudentInfo(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).showStudentInfo();
        }
    }

    //判断id是否存在
    public static boolean idIsExist(ArrayList<Student> list,String id){
        int index = getIndex(list,id);
        if(index > -1)return true;
        return false;
    }

    //返回id对应的索引
    public static int getIndex(ArrayList<Student> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    //显示登录界面
    public static void showLogin(){
        System.out.println("欢迎来到学生管理系统");
        System.out.println("请选择操作1登录 2注册 3忘记密码");
        System.out.println("请输入你的选择:");
    }

    //登录
    public static boolean login(ArrayList<User> userList){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        int index = getUserIndex(userList,username);
        if(index > -1){
            int count = 3;
            while (count != 0) {
                System.out.println("请输入密码");
                String password = sc.next();
                while (true) {
                    String num = getRandomNumber();
                    System.out.println("你的验证码为:" + num);
                    System.out.println("请输入验证码:");
                    String inputNum = sc.next();
                    if (num.equals(inputNum)) {
                        break;
                    } else System.out.println("验证码错误!请重新输入");
                }
                if(password.equals(userList.get(index).getPassword())){
                    System.out.println("登录成功!");
                    return true;
                }
                else {
                    count--;
                    System.out.println("密码错误!请重新登录");
                }
            }
            System.out.println("输入错误次数达到三次!无法继续输入");
            return false;
        }
        else System.out.println("登陆失败!用户名不存在,请注册后再来尝试!");
        return false;
    }

    //注册
    public static void register(ArrayList<User> userList){
        Scanner sc = new Scanner(System.in);
        String username , p1 ,idcard ,phoneNumber;
        while (true) {
            System.out.println("请输入您的用户名");
            username = sc.next();
            if(usernameIsTrue(userList,username)){
                break;
            }
            else System.out.println("创建失败!");
        }
        while (true) {
            System.out.println("请输入密码");
            p1 = sc.next();
            System.out.println("请确认密码");
            String p2 = sc.next();
            if(p1.equals(p2)){
                break;
            }
            else System.out.println("两次密码不一致!请重新输入");
        }
        while (true) {
            System.out.println("请输入身份证号");
            idcard = sc.next();
            if(idcardIsTrue(idcard)){
                break;
            }
            else System.out.println("身份证号不合法!请重新输入");
        }
        while (true) {
            System.out.println("请输入手机号");
            phoneNumber = sc.next();
            if(phoneNumberIsTrue(phoneNumber)){
                break;
            }
            else System.out.println("手机号不合法!请重新输入");
        }
        User newUser = new User(username,p1,idcard,phoneNumber);
        userList.add(newUser);
        System.out.println("注册成功!");
    }

    //忘记密码
    public static void setPassword(ArrayList<User> userList){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入用户名");
        String username = sc.next();
        int index = getUserIndex(userList,username);
        if(index != -1){
            System.out.println("输入您的身份证号");
            String idcard = sc.next();
            System.out.println("输入你的手机号");
            String phoneNumber = sc.next();
            if(userList.get(index).getIdCard().equals(idcard) && userList.get(index).getPhoneNumber().equals(phoneNumber)) {
                while (true) {
                    System.out.println("输入新的密码");
                    String newPassword = sc.next();
                    System.out.println("确认密码");
                    String p = sc.next();
                    if (newPassword.equals(p)){
                        userList.get(index).setPassword(p);
                        System.out.println("修改成功!");
                        break;
                    }
                    else System.out.println("两次输入密码不一致!请重新输入");
                }
            }
            else System.out.println("身份证号或手机号输入错误!");
        }
        else System.out.println("用户不存在!");
    }

    //判断身份证号码是否合法
    public static boolean idcardIsTrue(String idcard){
        if(idcard.length() != 18 || idcard.charAt(0) == '0')return false;
        if(idcard.charAt(17) != 'x' && idcard.charAt(17)!='X' && idcard.charAt(17) < '0' && idcard.charAt(17) > '9'){
            return false;
        }
        for (int i = 0; i < idcard.length() - 1; i++) {
            if(idcard.charAt(i) < '0' && idcard.charAt(i) > '9')return false;
        }
        return true;
    }

    //判断手机号是否合法
    public static boolean phoneNumberIsTrue(String phoneNumber){
       if(phoneNumber.length() != 11 || phoneNumber.charAt(0) == 0)return false;
        for (int i = 0; i < phoneNumber.length(); i++) {
            if(phoneNumber.charAt(i) < '0' || phoneNumber.charAt(i) > '9')return false;
        }
        return true;
    }

    //判断用户名是否合法
    public static boolean usernameIsTrue(ArrayList<User> userList,String username){
        if(username.length() < 3){
            System.out.println("用户名太短!");
            return false;
        }
        if(username.length() > 15){
            System.out.println("用户名太长!");
            return false;
        }
        if(getUserIndex(userList,username) != -1){
            System.out.println("用户名已存在!");
            return false;
        }
        boolean flag1 = false , flag2 = true;
        for (int i = 0; i < username.length(); i++) {
            if(username.charAt(i) >= 'a' && username.charAt(i) <= 'z')flag1 = true;
            if(username.charAt(i) < 48 || (username.charAt(i) > 57 && username.charAt(i)<64)
        ||(username.charAt(i)>90 && username.charAt(i) < 96)||username.charAt(i)>122){
                flag2 = false;
            }
        }
        if(flag1 && flag2) return true;
        else if(flag1) System.out.println("用户名含有不法字符!");
        else if(flag2) System.out.println("用户名不能全为数字!");
        return false;
    }

    //返回存在用户名对应的索引
    public static int getUserIndex(ArrayList<User> userList,String username){
        for (int i = 0; i < userList.size(); i++) {
            if(username.equals(userList.get(i).getUsername())){
                return i;
            }
        }
        return -1;
    }

    //生成随机验证码
    public static String getRandomNumber(){
        char[] ch = new char[52];
        for (int i = 0; i < ch.length; i++) {
            if(i <= 25){
                ch[i] = (char)('a' + i);
            }
            else{
                ch[i] = (char)('A' + i - 26);
            }
        }
        StringBuilder result  = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(ch.length);
            result.append(ch[randomIndex]);
        }
        int randomNum = r.nextInt(10);
        result.append(randomNum);
        int randomIndex = r.nextInt(5);
        StringBuilder ans = new StringBuilder();
        int i = 0,now = 0;
        while(ans.length() != 5){
            if(now != randomIndex){
                ans.append(result.charAt(i));
                i++;
                now++;
            }
            else{
                ans.append(randomNum);
                now++;
            }
        }
        return ans.toString();
    }
}



static

static表示静态,是Java中的一个修饰符,可以修饰成员方法,成员变量。
特点:

  • 被该类所有对象共享
  • 不属于对象,属于类
  • 随着类的加载而加载,优先于对象存在

调用方式:

  • 类名调用(推荐)
  • 对象名调用

工具类

帮助我们做一些事情的,但是不描述任何事物的类

  • 类名见名知意
  • 私有化构造方法

一个工具类ArrayUtil

package me.JavaStudy;

public class ArrayUtil {
    private ArrayUtil() {
    }

    //打印数组
    public static void printArr(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1)System.out.print(arr[i] + "]");
            else System.out.print(arr[i] + ", ");
        }
        System.out.println();
    }

    //求平均值
    public static double getAverage(double[] arr){
        double aver = 0.0;
        for (int i = 0; i < arr.length; i++) {
            aver += arr[i];
        }
        return aver/arr.length;
    }
}

继承

  • java提供一个关键字expense。用这个关键词可以让一个类和另一个类建立继承关系
    public class Student extends Person() {}
  • Stduent称为子类(派生类),Person称为父类(基类或者超类)

使用继承的好处

  • 可以把多个子类中重复的代码抽取到父类中了。提高代码的复用性

  • 子类可以在父类的基础上增加其他的功能,使子类更强大

  • java只支持单继承,不支持多继承,但支持多层继承

  • Java所有的类都直接或间接地继承object类

  • 子类只能访问父类中非私有的成员

子类到底能继承父类中的哪些内容

  1. 构造方法(非私有 不能 private 不能)
  2. 成员变量 (非私有 能 private 能)
  3. 成员方法(虚方法表 能 否则不能)

标签:String,int,日历,第三周,System,学习,println,public,out
From: https://www.cnblogs.com/XYu1230/p/18313611

相关文章

  • acwing学习笔记-数学知识
    文章目录数学知识一、质数1、试除法判定质数2、开方判定质数3、分解质因数4、筛质数(1)、埃氏筛法(2)、线性筛二、约数1、试除法求约数2、约数个数总结数学知识数学真是一个令人摸不着头脑的一个东西,小小的质数都可以把你拿捏得死死的一、质数1、试除法判定质......
  • 自学Java第三周
    本周学习一、循环1.while循环先判断条件,符合再执行。while(){}2.dowhile循环先执行一次,再判断条件。do{}while();3.forfor(初始条件;判断条件;循环迭代语句){}4.嵌套循环各种类型的循环都可以做内外侧循环。for(){for(){......;}}for(){while(){......;}}...5.控制循环结构break:完全结束一个循环,跳出循环体。continue:结束本次循环,开始下次循......
  • 【学习笔记】线段树优化建图
    前言2023.5.31贺了线段树优化建图板子。当时那段时间还被\(bobo\)一顿乱\(D\),让我多写点\(DP\),数学,少些点重复的数据结构。2024.7.19没想到暑假集训CSP提高模拟2\(T3\)放了个线段树优化建图板子,加上之前线段树优化建图代码是贺的,今年寒假本想找时间步一下的结果没去......
  • Hadoop学习总结
    今天建民老师对我们进行了小学期20天的Hadoop学习进行了测验,我只实现了数据库的导入,没完成数据的插入,会继续学习Hadoop。在学习Hadoop的过程中,我深入研究了其核心组成部分之一——Hadoop分布式文件系统(HDFS)。HDFS作为Hadoop生态系统的重要组成部分,其设计和工作原理让我大开眼界。......
  • 学习笔记第六天
    1.循环结构概述 定义:在给定条件成立时,反复执行某程序段; 要素:循环变量初始化语句;            循环的执行条件;      有使循环趋于结束的语句;2.while循环 格式:while(表达式)语句;特点:先判断条件,后执行语句3.do-while循环格式:do语句whi......
  • 第三周
    idea连接hive一、准备工作1、添加依赖添加hive-jdbc和hadoop-common的依赖。注意:此处添加的依赖要和自己服务器上的版本对应上。<!--添加所用hive版本的jdbc依赖--><dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><vers......
  • 第三周总结
    学习时间:每天3h代码量:每日300行今日进行放假的测试石家庄铁道大学2024年夏季  2022级课堂测试试卷—Hadoop数据库应用课程名称:大型数据库应用技术 任课教师:王建民  考试时间:实现为止分钟  一、    测试题目1、完成科技查新基本信息填报功能,基本信息如下所示......
  • 第三周
    本周学习了判断与循环以及数组;了解了java与c++语言的相通之处;并进行了相关例题训练:完成pta部分习题下周进行方法以及面向对象的学习;......
  • Day3 变量赋初值,类运算符和表达式,不同数值型数据间的混合运算混合赋值学习
    算术运算符和算术表达式,赋值运算符和赋值表达式以及逗号运算符和逗号表达式一、变量赋初值变量赋初值就是在为变量开辟空间时为其进行初始化的操作称之为变量赋初值。变量赋虽然比较简单理解,但有许多人不清楚变量赋初值与变量赋值有什么区别,虽然都是使用“=”完成的,但二者并......
  • vue3 - 最新详细实现 “拖曳式课程表“ 日历课程排班功能,用鼠标拖拽课程放到日历课表
    效果图在vue3、nuxt3项目开发中,详解实现用鼠标拖拽排课的日历课程表,拖动课程名(学科)到日历课程表的某一节课中,拖放到课表上的时候自动沿边吸附贴合,vue3可视化拖拽生成课程表插件,左边是科目课程、右边是教学周课表,鼠标拖动把课程放到指定的日期时间上即可快速生成课表,数据......