<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[success] # ArrayList 增刪改查案例 1. List 作為 ArrayList的接口 實現了List 中接口的方法,同樣具備一下操作 2. `ArrayList() `,創建一個長度為0的集合 * `boolean add(E e) 添加` * `boolean remove(E e) 刪除` * `E remove(int index) 刪除指定位置` * `E set(int index,E e) 修改` * `E get(int index) 查詢` * `int size() 獲取長度` ~~~ import java.util.ArrayList; public class ArrayListTest { public static void main(String[] args) { /* * boolean add(E e) 添加 * * boolean remove(E e) 刪除 * E remove(int index) * * E set(int index,E e) 修改 * * E get(int index) 查詢 * int size() 獲取長度 * */ ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); // 刪除內容 boolean remove1 = list.remove("aaa"); System.out.print(remove1); // true // 通過序號刪除內容 String rm = list.remove(0); System.out.println(rm); // bbb // 修改指定位置內容 String update = list.set(0, "123"); System.out.print(update); // ccc 返回被修改內容 // 獲取指定位置內容 String str = list.get(0); System.out.print(str); // 123 // 獲取長度 int len = list.size(); System.out.print(len); // 2 } } ~~~ >[danger] ## 集合中包裝類 1. 數組可以保存基本類型或者引用類型,集合是引用類型,如果保存基本類型需要使用其**包裝類** ~~~ import java.util.ArrayList; public class ArrayListTest { public static void main(String[] args) { // 定義保存int 類型數據 就需要使用int 的包裝類Integer ArrayList<Integer> intLs = new ArrayList<>(); // 增 intLs.add(1); intLs.add(2); // 刪 指定內容值 Integer i = 1; intLs.remove(i); // 刪除指定位置的值 intLs.remove(0); // 獲取長度 int size = intLs.size(); System.out.print(size); // 0 // 新增后更新值 intLs.add(789); intLs.set(0, 1223456); // 打印值 Integer ii = intLs.get(0); System.out.print(ii); // 1223456 } } ~~~ >[info] ## 簡單學生管理 1. 具備可以手動輸入學生數據 2. 可以根據學號查找某個學生是否存在 3. 可以打印出某個學號學生的具體信息 4. 可以統計成績不及格人數據打印 5. 可以打印所有學生數據 >[danger] ##### 創建學生的javaBean -- Student ~~~ public class Student { // 私有化成員變量 private int id; private String name; private int age; private int scores; // 構造方法 public Student(int id, String name, int age, int scores) { this.id = id; this.name = name; this.age = age; this.scores = scores; } public Student() { } // get/set方法 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; } public int getScores() { return scores; } public void setScores(int scores) { this.scores = scores; } } ~~~ >[danger] ##### 實現功能的方法類 -- StudentTest ~~~ import java.util.ArrayList; import java.util.Scanner; import javax.swing.text.html.HTMLDocument.RunElement; /** * * 1. 具備可以手動輸入學生數據 * 2. 可以根據學號查找某個學生是否存在 * 3. 可以打印出某個學號學生的具體信息 * 4. 可以統計成績不及格人數據打印 * 5. 可以打印所有學生數據 */ public class StudentTest { public static void main(String[] args) { // 開始錄入 Scanner sc = new Scanner(System.in); System.out.println("請輸入錄入人數"); int count = sc.nextInt(); // 獲取錄入list ArrayList<Student> students = enterStudentsInfo(count); // 輸入要查找id System.out.println("輸入要查找id"); int id = sc.nextInt(); if (contains(students, id)) { int index = getIndexById(students, id); Student stu = students.get(index); System.out.print(stu.getName()); } else { System.out.println("當前學生不存在"); } } // 錄入學生 public static ArrayList<Student> enterStudentsInfo(int count) { Scanner sc = new Scanner(System.in); // 創建一個錄入集合 ArrayList<Student> studentLs = new ArrayList<>(); for (int i = 0; i < count; i++) { Student stu = new Student(); System.out.println("輸入學生ID"); int Id = sc.nextInt(); stu.setId(Id); System.out.println("請輸入學生名字"); String name = sc.next(); stu.setName(name); System.out.println("請輸入學生年齡"); int age = sc.nextInt(); stu.setAge(age); System.out.print("請輸入學生分數"); double scores = sc.nextDouble(); stu.setScores(scores); // 保存學生 studentLs.add(stu); } return studentLs; } // 根據Id 獲得索引位置 public static int getIndexById(ArrayList<Student> arr, int id) { for (int i = 0; i < arr.size(); i++) { Student stu = arr.get(i); if (stu.getId() == id) { return i; } } return -1; } // 是否包含 public static boolean contains(ArrayList<Student> arr, int id) { return getIndexById(arr, id) >= 0; } // 獲取不及格信息 public static ArrayList<Student> getLtSources60(ArrayList<Student> arr) { ArrayList<Student> stuLs = new ArrayList<>(); for (int i = 0; i < arr.size(); i++) { Student stu = arr.get(i); double sources = stu.getScores(); if (sources < 60) { stuLs.add(stu); } } return stuLs; } } ~~~ >[info] ## 用戶登錄注冊 1. 學生管理系統書寫一個登陸、注冊、忘記密碼的功能 2. 需求文檔: * 用戶類: 屬性:用戶名、密碼、身份證號碼、手機號碼 * 注冊功能: 用戶名唯一 用戶名長度必須在3~15位之間 只能是字母加數字的組合,但是不能是純數字 密碼鍵盤輸入兩次,兩次一致才可以進行注冊。 身份證號碼需要驗證,**長度為18位,不能以0為開頭,前17位,必須都是數字,最后一位可以是數字,也可以是大寫X或小寫x** 手機號驗證**長度為11位不能以0為開頭必須都是數字** * 登錄功能:鍵盤錄入用戶名,鍵盤錄入密碼,鍵盤錄入驗證碼 **驗證要求**:用戶名如果未注冊,直接結束方法,并提示:用戶名未注冊,請先注冊 ,判斷驗證碼是否正確,如不正確,重新輸入再判斷用戶名和密碼是否正確,有3次機會 * 忘記密碼:鍵盤錄入用戶名,判斷當前用戶名是否存在,如不存在,直接結束方法,并提示:未注冊。鍵盤錄入身份證號碼和手機號碼,判斷當前用戶的身份證號碼和手機號碼是否一致,如果一致,則提示輸入密碼,進行修改。如果不一致,則提示:賬號信息不匹配,修改失敗。 * 驗證碼規則:長度為5,由4位大寫或者小寫字母和1位數字組成,同一個字母可重復 數字可以出現在任意位置 >[danger] ##### 創建用戶的javaBean類 -- User ~~~ public class User { // 定義私有變量 private String name; private String password; private String personID; private String phoneNum; // 創建有參無參構造 public User() { } public User(String name, String password, String personID, String phoneNum) { this.name = name; this.password = password; this.personID = personID; this.phoneNum = phoneNum; } // 使用訪問的get set 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPersonID() { return personID; } public void setPersonID(String personID) { this.personID = personID; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } } ~~~ >[danger] ##### 登錄界面 ~~~ import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class UserTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 保存記錄的list ArrayList<User> ls = new ArrayList<>(); while (true) { // 繪制注冊頁面 System.out.println("歡迎來到學生管理系統"); System.out.println("請選擇操作:1登錄 2注冊 3忘記密碼"); // 開始進行分配 String choose = sc.next(); switch (choose) { case "1" -> login(ls); case "2" -> register(ls); case "3" -> forgetPassword(ls); case "4" -> { System.out.println("謝謝使用,再見"); System.exit(0);// 強制退出 } default -> System.out.println("沒有這個選項"); } } } private static void printList(ArrayList<User> list) { for (int i = 0; i < list.size(); i++) { // i 索引 User user = list.get(i); System.out.println(user.getName() + ", " + user.getPassword() + ", " + user.getPersonID() + ", " + user.getPhoneNum()); } } public static void login(ArrayList<User> list) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("請輸入用戶名"); String username = sc.next(); // 判斷用戶名是否存在 boolean flag = contains(list, username); if (!flag) { System.out.println("用戶名" + username + "未注冊,請先注冊再登錄"); return; } System.out.println("請輸入密碼"); String password = sc.next(); while (true) { String rightCode = getCode(); System.out.println("當前正確的驗證碼為:" + rightCode); System.out.println("請輸入驗證碼"); String code = sc.next(); if (code.equalsIgnoreCase(rightCode)) { System.out.println("驗證碼正確"); break; } else { System.out.println("驗證碼錯誤"); continue; } } // 驗證用戶名和密碼是否正確 // 集合中是否包含用戶名和密碼 // 定義一個方法驗證用戶名和密碼是否正確 // 封裝思想的應用: // 我們可以把一些零散的數據,封裝成一個對象 // 以后傳遞參數的時候,只要傳遞一個整體就可以了,不需要管這些零散的數據。 User useInfo = new User(username, password, null, null); boolean result = checkUserInfo(list, useInfo); if (result) { System.out.println("登錄成功,可以開始使用學生管理系統了"); break; } else { System.out.println("登錄失敗,用戶名或密碼錯誤"); if (i == 2) { System.out.println("當前賬號" + username + "被鎖定,請聯系黑馬程序員客服:XXX-XXXXX"); // 當前賬號鎖定之后,直接結束方法即可 return; } else { System.out.println("用戶名或密碼錯誤,還剩下" + (2 - i) + "次機會"); } } } } public static void register(ArrayList<User> ls) { Scanner sc = new Scanner(System.in); /* * 校驗用戶名 * 用戶名長度必須在3~15位之間 * 只能是字母加數字的組合,但是不能是純數字 * * 整個用戶名也可能不是一次輸入對的因此需要循環 */ String username; while (true) { System.out.println("請輸入用戶名稱"); username = sc.next(); boolean flagName = checkUsername(username); // 不滿足需要重新輸入用戶名 if (!flagName) { System.out.println("用戶名格式不滿足條件,需要重新輸入"); continue; } // 校驗用戶名是否唯一 boolean flag2 = contains(ls, username); if (flag2) { // 用戶名已存在,那么當前用戶名無法注冊,需要重新輸入 System.out.println("用戶名" + username + "已存在,請重新輸入"); } else { // 不存在,表示當前用戶名可用,可以繼續錄入下面的其他數據 System.out.println("用戶名" + username + "可用"); break; } } // 密碼鍵盤輸入兩次,兩次一致才可以進行注冊 String pwd; while (true) { System.out.println("請輸入要注冊的密碼"); pwd = sc.next(); System.out.println("請再次輸入要注冊的密碼"); String aginPwd = sc.next(); if (!pwd.equals(aginPwd)) { System.out.println("兩次密碼輸入不一致,請重新輸入"); continue; } System.out.println("兩次密碼一致,繼續錄入其他數據"); break; } // 身份證號碼需要驗證。 // ? 驗證要求: // ? 長度為18位 // ? 不能以0為開頭 // ? 前17位,必須都是數字 // ? 最為一位可以是數字,也可以是大寫X或小寫x String id; while (true) { System.out.println("請輸入身份證號碼"); id = sc.next(); if (checkPersonId(id)) { break; } else { System.out.println("身份證號碼格式有誤,請重新輸入"); continue; } } // 手機號驗證。 // ? 驗證要求: // ? 長度為11位 // ? 不能以0為開頭 // ? 必須都是數字 String phoneNum; while (true) { System.out.println("請輸入手機號碼"); phoneNum = sc.next(); boolean flag = checkPhoneNum(phoneNum); if (flag) { break; } else { System.out.println("手機號碼格式有誤,請重新輸入"); } } // 用戶名,密碼,身份證號碼,手機號碼放到用戶對象中 User u = new User(username, pwd, id, phoneNum); // 把用戶對象添加到集合中 ls.add(u); System.out.println("注冊成功"); printList(ls); }; public static void forgetPassword(ArrayList<User> list) { Scanner sc = new Scanner(System.in); System.out.println("請輸入用戶名"); String username = sc.next(); boolean flag = contains(list, username); if (!flag) { System.out.println("當前用戶" + username + "未注冊,請先注冊"); return; } // 鍵盤錄入身份證號碼和手機號碼 System.out.println("請輸入身份證號碼"); String personID = sc.next(); System.out.println("請輸入手機號碼"); String phoneNumber = sc.next(); // 需要把用戶對象通過索引先獲取出來。 int index = findIndex(list, username); User user = list.get(index); // 比較用戶對象中的手機號碼和身份證號碼是否相同 if (!(user.getPersonID().equalsIgnoreCase(personID) && user.getPhoneNum().equals(phoneNumber))) { System.out.println("身份證號碼或手機號碼輸入有誤,不能修改密碼"); return; } // 當代碼執行到這里,表示所有的數據全部驗證成功,直接修改即可 String password; while (true) { System.out.println("請輸入新的密碼"); password = sc.next(); System.out.println("請再次輸入新的密碼"); String againPassword = sc.next(); if (password.equals(againPassword)) { System.out.println("兩次密碼輸入一致"); break; } else { System.out.println("兩次密碼輸入不一致,請重新輸入"); continue; } } // 直接修改即可 user.setPassword(password); System.out.println("密碼修改成功"); } private static int findIndex(ArrayList<User> list, String username) { for (int i = 0; i < list.size(); i++) { User user = list.get(i); if (user.getName().equals(username)) { return i; } } return -1; } private static boolean checkUserInfo(ArrayList<User> list, User useInfo) { // 遍歷集合,判斷用戶是否存在,如果存在登錄成功,如果不存在登錄失敗 for (int i = 0; i < list.size(); i++) { User user = list.get(i); if (user.getName().equals(useInfo.getName()) && user.getPassword().equals(useInfo.getPassword())) { return true; } } return false; } // 校驗手機號 public static boolean checkPhoneNum(String phoneNum) { int len = phoneNum.length(); if (len != 11) { return false; } if (phoneNum.startsWith("0")) { return false; } for (int i = 0; i < len; i++) { char c = phoneNum.charAt(i); if (c < '0' || c > '9') { return false; } } return true; } // 校驗身份證號 public static boolean checkPersonId(String id) { int len = id.length(); if (len != 18) { return false; } if (id.startsWith("0")) { return false; } // 第18 位 比較特殊 可以先校驗 17 位 for (int i = 0; i < len; i++) { char c = id.charAt(i); // 如果有一個字符不在0-9之間,那么直接返回false if (!(c >= '0' && c <= '9')) { return false; } } // 單獨校驗18位置 char endC = id.charAt(id.length() - 1); if (!(endC >= '0' && endC <= '9') && endC != 'x' && endC != 'X') { return false; } return true; } // 校驗用戶名 public static boolean checkUsername(String userName) { /* * 用戶名長度必須在3~15位之間 * 只能是字母加數字的組合,但是不能是純數字 */ int len = userName.length(); if (len < 3 || len > 15) { return false; } // 判斷是否除了數字和字母 for (int i = 0; i < len; i++) { char c = userName.charAt(i); if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) { return false; } } // 要看看實際是否包含英文 int count = 0; for (int i = 0; i < len; i++) { // i 索引 char c = userName.charAt(i); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { count++; break; } } return count > 0; } // 是否包含 public static boolean contains(ArrayList<User> ls, String name) { for (int i = 0; i < ls.size(); i++) { User user = ls.get(i); if (user.getName().equals(name)) { return true; } } return false; } // 生成驗證碼 public static String getCode() { // 生成 a-z 和 A-z 的字符串ls ArrayList<Character> list = new ArrayList<>(); for (int i = 0; i < 26; i++) { list.add((char) ('a' + i)); list.add((char) ('A' + i)); } StringBuffer sb = new StringBuffer(); Random r = new Random(); // 隨機從ls 中取值 for (int i = 0; i < 4; i++) { // 獲取隨機索引 int index = r.nextInt(list.size()); // 利用隨機索引獲取字符 char c = list.get(index); // 把隨機字符添加到sb當中 sb.append(c); } int number = r.nextInt(10); sb.append(number); // 數字是隨機位置 // 先把字符串變成字符數組,在數組中修改,然后再創建一個新的字符串 char[] arr = sb.toString().toCharArray(); // 拿著最后一個索引,跟隨機索引進行交換 int randomIndex = r.nextInt(arr.length); // 最大索引指向的元素 跟隨機索引指向的元素交換 char temp = arr[randomIndex]; arr[randomIndex] = arr[arr.length - 1]; arr[arr.length - 1] = temp; return new String(arr); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看