大家可以关注作者的账号,关注从零开始学Java笔记文集。也可以根据目录前往作者的博客园博客进行学习。本片文件将基于黑马程序员就业班视频进行学习以及资料的分享,并记录笔记和自己的看法。欢迎大家一起学习和讨论。
【从零开始学Java笔记】目录
学生管理系统一直是大学学习过程中无法跳过的环节,因为这个系统囊括了很多知识,是初学者不错的上手项目。
需求:
1.学生属性有学号,姓名,年龄和地址
2.要有主界面
3.可以增加学生,学号唯一,不能重复添加
4.可以查看所有学生
5.可以根据学号删除学生
6.可以根据学号查找学生
实现:
ust.studentmanagement;public class Student {//成员变量private String id;private String name;private String age;private String address;//get、set方法public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}//无参构造public Student() {super();// TODO Auto-generated constructor stub}//有参构造public Student(String id, String name, String age, String address) {super();this.id = id;this.name = name;this.age = age;this.address = address;}
}
ust.studentmanagement;import java.util.ArrayList;
import java.util.Scanner;/** */
public class StudentAction {public static void main(String[] args) {// 创建一个集合ArrayList<Student> stu = new ArrayList<Student>();// 主界面,while(true)是一个死循环while (true) {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("请输入您需要的操作:");// 从键盘输入Scanner sc = new Scanner(System.in);// 接收int choice = sc.nextInt();// switch选择switch (choice) {case 1:addStudent(stu);break;case 2:travelStudent(stu);break;case 3:deleteStudent(stu);break;case 4:modifyStudent(stu);break;// 这个地方用了case穿透,即case 5没有内容,直接穿透到下一个casecase 5:default:System.out.println("谢谢您的使用!");// 关闭虚拟机,结束程序it(0);}}}// 增加学生public static void addStudent(ArrayList<Student> stu) {// 键盘输入Scanner sc = new Scanner(System.in);// 提拉,将id的声明提拉至循环外,以便循环后还能使用,如果在循环内声明,循环结束后,变量也随之消失String id;// while循环,判断学号是否重复while (true) {System.out.println("请输入学号:");id = sc.nextLine();// 判断标志初始化boolean index = false;// 循环判断,如果发现相同,改变判断标志,跳出循环for (int i = 0; i < stu.size(); i++) {if (id.(i).getId())) {index = true;break;}}// 判断if (index) {System.out.println("您输入的学号已存在!请重新输入。");System.out.println("如需重新输入学生信息,请按1");System.out.println("如需回主菜单,请按2");System.out.println("请输入您需要的操作:");Scanner sc1 = new Scanner(System.in);// 接收int flag = Int();// switch选择switch (flag) {case 1:// 跳出判断继续循环break;case 2:default:// return直接结束方法return;}} elsebreak;}// 输入其他信息,并添加至集合System.out.println("请输入姓名:");String name = sc.nextLine();System.out.println("请输入年龄:");String age = sc.nextLine();System.out.println("请输入地址:");String address = sc.nextLine();Student s = new Student(id, name, age, address);stu.add(s);System.out.println("添加成功");}//查看所有叙述public static void travelStudent(ArrayList<Student> stu) {//如果没有学生显示没有学生,如果有显示学生信息if (stu.size() == 0) {System.out.println("没有学生");} else {System.out.println("学号tt姓名t年龄t地址t");for (int i = 0; i < stu.size(); i++) {Student s = (i);System.out.Id() + "tt" + s.getName() + "t" + s.getAge() + "t" + s.getAddress());}}}
//修改学生信息public static void modifyStudent(ArrayList<Student> stu) {System.out.println("请输入要修改学生的学号:");//接收学号Scanner sc = new Scanner(System.in);String id = sc.nextLine();//标志初始化boolean judge = false;int index = -1;//遍历寻找学生,找到修改标志for (int i = 0; i < stu.size(); i++) {if (id.(i).getId())) {judge = true;index = i;break;}}if (judge) {System.out.println("请重新输入姓名:");String name = sc.nextLine();System.out.println("请重新输入年龄:");String age = sc.nextLine();System.out.println("请重新输入地址:");String address = sc.nextLine();//新建一个对象Student s = new Student(id, name, age, address);//修改stu.set(index,s);System.out.println("修改成功");} else {System.out.println("没有这个学生!");}}
//删除学生信息public static void deleteStudent(ArrayList<Student> stu) {System.out.println("请输入要删除学生的学号:");//接收学生学号Scanner sc = new Scanner(System.in);String id = sc.nextLine();//标注初始化boolean judge = false;//遍历寻找for (int i = 0; i < stu.size(); i++) {if (id.(i).getId())) {//移除ve(i);judge = true;break;}}//判断if (judge) {System.out.println("删除成功!");} else {System.out.println("没有这个学生!");}}}
本文发布于:2024-01-29 10:55:35,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170649694014784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |