网站访问量突然增加,哪些网站做的美,360网站收录提交,互联网开发技术目录
学生管理系统
学生管理系统代码思路分析
nextLine() 和 nextInt() 区别 学生管理系统 需求#xff1a;实现对学生的增删改查功能#xff0c;学生#xff08;学号#xff0c;姓名#xff0c;年龄#xff0c;地址#xff09;字段 学生管理系统代码思路分析 定义学…目录
学生管理系统
学生管理系统代码思路分析
nextLine() 和 nextInt() 区别 学生管理系统 需求实现对学生的增删改查功能学生学号姓名年龄地址字段 学生管理系统代码思路分析 定义学生 Student 实体类 成员属性 学号姓名年龄地址定义容器ArrayList 集合存入对象定义StudentManage 对 Student对象集合实现增删改查CURD; 注学生的学号 存入学生对象集合 中 不允许重复 示例代码
Student.java类
package com.collection.Demo03;public class Student {private Integer stuId;//学号private String name;//姓名private Integer age;//年龄private String address;//地址public Integer getStuId() {return stuId;}public void setStuId(Integer stuId) {this.stuId stuId;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getAddress() {return address;}public void setAddress(String address) {this.address address;}public Student(Integer stuId, String name, Integer age, String address) {this.stuId stuId;this.name name;this.age age;this.address address;}
}StudentManage.java
package com.collection.Demo03;import java.util.ArrayList;
import java.util.Scanner;public class StudentManage {// 学生对象集合private static ArrayListStudent stus new ArrayListStudent();//该变量值可以控制循环是否继续执行private volatile static boolean isRun true; //学习到了多线程 volatile保证isRun线程可见性——方式1/*** 学生管理系统类 主程序执行的入口* 能够实现系统退出* 方式1: Boolean 变量 控制循环 改变位isRunfalse,则不会继续循环* 方式2: return; 循环就直接退出了* 方式3: System.exit(0);//停止jvm进程* 方式4: 在while内switch外if (number 5) {break;}* 方式5: 报错抛出异常程序终止*/public static void main(String[] args) {while (isRun) {System.out.println(欢迎来到学生管理系统);System.out.println(1.新增学生C);System.out.println(2.修改学生U);System.out.println(3.查询学生R);System.out.println(4.删除学生D);System.out.println(5.退出系统);System.out.print(请选择[1-5]:);Scanner scanner new Scanner(System.in);int number scanner.nextInt();//判断方式 switch() 或 ifswitch (number) {case 1:System.out.println(新增学生);addStudent();break;case 2:System.out.println(修改学生);updateStudent();break;case 3:System.out.println(查询学生);showStudent();break;case 4:System.out.println(删除学生);deleteStudent();break;case 5:System.out.println(感谢使用);
// isRunfalse;——方式1
// break;——方式1System.exit(0);//方式3——停止jvm进程return; //循环直接退出——方式2default:System.out.println(输入错误请重新输入);}
// if (number 5) {break;} //方式4}}/*** 封装提供方法 就是根据学号查询 学生对象集合中 学生信息* 如果能够查询到 则直接返回** param stuId 键盘录入的学号目的与已经从在集合中的学生学号比较* return 根据学号查询 学生对象集合中 学生信息 返回学生对象 不会删除学生对象(isDeletefalse)*/public static Student getByStuIdStudnet(Integer stuId) {return getByStuIdStudnet(stuId, false);}/*** 方法重载** param stuId 学号* param isDelete 是否删除该学生 isDeletetrue 删除该学生* return*/public static Student getByStuIdStudnet(Integer stuId, boolean isDelete) {for (int i 0; i stus.size(); i) {Student student stus.get(i);if (student.getStuId().equals(stuId)) {if (isDelete) {return stus.remove(i); //删除成功之后 就返回原来删除的对象}return student;}}return null;}/*** 新增学生*/public static void addStudent() {Scanner sc new Scanner(System.in);System.out.print(请输入学生学号:);Integer stuId sc.nextInt();sc.nextLine();// 根据学号从集合中查询 如果存在的话 就不允许插入该学号Student student getByStuIdStudnet(stuId);if (student ! null) {//在集合中查询到该学号已经被其他学生使用不能继续添加System.out.println(学号重复);return;//程序不会向下执行了}System.out.print(请输入学生姓名:);String name sc.nextLine();System.out.print(请输入学生年龄:);Integer age sc.nextInt();sc.nextLine(); //使用nextLine()消耗留在输入流中的换行符,或者使用下面的nextLine()后在使用parseInt()转换
// String ageStr sc.nextLine();
// Integer age Integer.parseInt(ageStr); // 将字符串转换为整数System.out.print(请输入学生地址:);String address sc.nextLine();//直接将学生的信息存入到集合中stus.add(new Student(stuId, name, age, address));System.out.println(添加成功!);}/*** 查询学生*/public static void showStudent() {if (stus.size() 0) {System.out.println(该集合中没有存入任何学生对象);return;//程序不会继续向下执行}System.out.println(学号\t\t姓名\t\t年龄\t\t地址);for (Student student : stus) {System.out.println(student.getStuId() \t\t student.getName() \t\t student.getAge() \t\t student.getAddress());}}/*** 删除学生*/public static void deleteStudent() {//删除学生 学号唯一不允许重复 根据学生学号删除System.out.print(请输入删除的学生学号:);Scanner sc new Scanner(System.in);Integer stuId sc.nextInt();//需要先根据该学号查找集合中 是否存在该对象
// for (int i 0; i stus.size(); i) {
// Student student stus.get(i);
// if (student.getStuId().equals(stuId)){
// stus.remove(i);//直接根据 index下标删除
// System.out.println(删除成功);
// return;//如果根据学号查找到该学生 该循环就不会继续执行
// }
// }Student studnet getByStuIdStudnet(stuId, true);if (studnet null) {System.out.println(集合中不存在该学生学号!);return;}System.out.println(删除成功 学号: stuId);
// System.out.println(集合中不存在该学生学号!);}/*** 修改学生信息*/public static void updateStudent() {System.out.print(请输入修改的学生学号:);Scanner sc new Scanner(System.in);Integer stuId sc.nextInt();sc.nextLine();//根据学号查找学生对象Student studnet getByStuIdStudnet(stuId);if (studnet null) {System.out.println(集合中不存在该学生);return;}//修改的学生对象 存在System.out.print(请输入学生的新的姓名:);String newName sc.nextLine();System.out.print(请输入学生的新的年龄:);Integer newAge sc.nextInt();sc.nextLine();System.out.print(请输入学生的新的地址:);String newAddress sc.nextLine();studnet.setName(newName);studnet.setAge(newAge);studnet.setAddress(newAddress);System.out.println(修改成功);}
}nextLine() 和 nextInt() 区别 nextLine()和nextInt()是Java中Scanner类的两个方法它们用于从输入流中读取不同类型的数据。 1. nextInt()方法用于读取整数类型的数据它会从输入流中读取数字字符并将其转换为整数。 这个方法在读取整数之后不会读取换行符因此如果连续调用nextInt()方法它们会读取同一个换行符之前的数字。 2. nextLine()方法用于读取整行文本包括空格和换行符。 这个方法会读取输入流中的换行符并将其作为字符串的一部分返回。 因此如果在调用nextInt()方法后使用nextLine()方法nextLine()方法会读取之前留在输入流中的换行符因此看起来像是没有接收到输入。 因此如果您想在调用nextInt()方法后使用nextLine()方法读取字符串您需要在nextInt()方法之后额外调用一次nextLine()方法以消耗留在输入流中的换行符。 或者将所有输入都使用nextLine()方法读取并将需要转换为整数的字符串使用Integer.parseInt()方法转换。 下一篇文章