Java基础 您所在的位置:网站首页 学生成绩管理java Java基础

Java基础

2024-02-16 19:19| 来源: 网络整理| 查看: 265

用内存存储学生信息。(采用集合的方式) 步骤: A. 定义学生类 B. 学生管理系统的主界面的代码编写 C. 学生管理系统的查看所有学生的代码编写 D. 学生管理系统的添加学生的代码编写 E. 学生管理系统的删除学生的代码编写 F. 学生管理系统的修改学生的代码编写

代码: 学生类:Student.java

public class Student { private String id; private String name; private String age; private String address; public Student(){} public Student(String id, String name, String age, String address) { this.id = id; this.name = name; this.age = age; this.address = address; } 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; } } import java.util.ArrayList; import java.util.Scanner; public class StudentManage { public static void main(String[] args) { ArrayList array = new ArrayList(); while(true) { //停止的另一种方式,设置标志位flag = true while(flag) 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.print("请输入您的选择:"); Scanner sc = new Scanner(System.in); String choice = sc.nextLine(); switch (choice) { case "1": //查看所有学生 findAllStudent(array); System.out.println(); break; case "2": //添加学生 addStudent(array); System.out.println(); break; case "3": deleteStudent(array); System.out.println(); break; case "4": updateStudent(array); System.out.println(); break; case "5": default: // System.out.println("------ 5. 退出 -----"); System.out.println(" 感谢您的使用 "); //flag = false; System.exit(0); break; } } } //查看所有学生 public static void findAllStudent(ArrayList array){ if(array.size() == 0){ System.out.println("无学生信息,请重新选择"); return; } System.out.println(); System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄\t\t\t\t居住地"); System.out.println("-------------------------------------------------------------------------"); for(int x = 0; x Scanner in = new Scanner(System.in); String id =""; while(true) { System.out.print("输入学号:"); id = in.nextLine(); int x; boolean flag = false; for(x = 0; x flag = true; break; } } if(flag){ System.out.println("学号被占用,请重新输入"); } else{ break; } } System.out.print("输入姓名:"); String name = in.nextLine(); System.out.print("输入年龄:"); String age = in.nextLine(); System.out.print("输入地址:"); String address = in.nextLine(); //创建学生对象 Student s = new Student(id,name,age,address); /* Student s = new Student(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); */ array.add(s); System.out.println("添加学生成功"); } //删除学生 public static void deleteStudent(ArrayList array){ //思路:根据学号删除。先遍历学生列表查看学生是否存在 Scanner in = new Scanner(System.in); System.out.print("输入要删除学生的学号:"); String id = in.nextLine(); int index = -1; for(int x = 0; x index = x; break; } } if(index == -1){ System.out.print("该学生信息不存在,是否继续当前删除操作(Yes or No):"); String choice = in.nextLine(); if(choice.equalsIgnoreCase("Yes")){ deleteStudent(array); }else if(choice.equalsIgnoreCase("No")) { return; } else{ System.out.println("输入信息有误,返回主界面"); return; } }else { array.remove(index); System.out.println("删除学生成功"); } } //修改学生信息 public static void updateStudent(ArrayList array){ //输入学号查找学生是否存在 Scanner in = new Scanner(System.in); System.out.print("输入要更新学生的学号:"); String id = in.nextLine(); int index = -1; for(int x = 0; x index = x; break; } } if(index == -1){ System.out.print("该学生信息不存在,是否继续当前更新操作(Yes or No):"); String choice = in.nextLine(); if(choice.equalsIgnoreCase("Yes")){ updateStudent(array); }else if(choice.equalsIgnoreCase("No")) { return; } else{ System.out.println("输入信息有误,返回主界面"); return; } }else { Student s = array.get(index); boolean flag = true; System.out.println("-----选择更新学生信息选项------"); System.out.println("-- 1. 姓名 --"); System.out.println("-- 2. 年龄 --"); System.out.println("-- 3. 地址 --"); System.out.println("-- 4. 返回主界面 --"); System.out.println("--------------------------"); while(flag) { System.out.print("请输入您的选择:"); Scanner sc = new Scanner(System.in); String choice = sc.nextLine(); switch (choice) { case "1": System.out.print("输入修改姓名:"); String name = in.nextLine(); s.setName(name); System.out.print("继续更新操作(Yes or No):"); String a = in.nextLine(); if(a.equalsIgnoreCase("Yes")) break; else if(a.equalsIgnoreCase("No")){ array.set(index,s); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "2": System.out.print("输入修改年龄:"); String age = in.nextLine(); s.setAge(age); System.out.print("继续更新操作(Yes or No):"); String b = in.nextLine(); if(b.equalsIgnoreCase("Yes")) break; else if(b.equalsIgnoreCase("No")){ array.set(index,s); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "3": System.out.print("输入修改地址:"); String address = in.nextLine(); s.setAddress(address); System.out.print("继续更新操作(Yes or No):"); String c = in.nextLine(); if(c.equalsIgnoreCase("Yes")) break; else if(c.equalsIgnoreCase("No")){ array.set(index,s); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "4": array.set(index,s); System.out.println("修改信息完成。"); return; default: System.out.println("输入信息有误。返回主界面"); return; } } } } }

无信息,查看信息时: 在这里插入图片描述

添加学生: 在这里插入图片描述 在这里插入图片描述 在查看信息: 在这里插入图片描述 删除学生: 在这里插入图片描述 修改学生: 在这里插入图片描述 在这里插入图片描述

退出: 在这里插入图片描述

文本存储学生信息

学生类:

public class Student { private String id; private String name; private String age; private String address; public Student(){} public Student(String id, String name, String age, String address) { this.id = id; this.name = name; this.age = age; this.address = address; } 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; } }

思路: 在这里插入图片描述

(1)输出流:将集合中的内容输出到文本文件中 集合写入文本文件: 需求:键盘录入学生信息(学号,姓名,年龄,居住地)存入集合。然后遍历集合把每一个学生信息存入文本文件(每一个学生信息为一行数据,自己定义分割标记)

分析: A. 定义学生类 B. 创建集合对象 C. 写方法实现键盘录入学生信息,并把学生对象作为元素添加集合 D. 创建输出缓冲流对象 E. 遍历集合,得到每一个学生信息,并把学生信息按照一定的格式写入文本文件。(123456,wang,22,wuhan) F. 释放资源

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; public class WriteDemo { public static void main(String[] args) throws IOException { ArrayList array = new ArrayList(); Student s1 = new Student("123456","wang","22","wuhan"); Student s2 = new Student("123457","zhang","28","beijing"); Student s3 = new Student("123458","lisi","20","wixi"); array.add(s1); array.add(s2); array.add(s3); BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt")); for(int x = 0; x public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("array.txt")); ArrayList array = new ArrayList(); String line; while((line = br.readLine())!= null){ String[] strArray = line.split(","); Student s = new Student(strArray[0],strArray[1],strArray[2],strArray[3]); array.add(s); } br.close(); System.out.println("学号\t\t姓名\t年龄\t居住地"); for(int x = 0; x public static void main(String[] args) throws IOException { String fileName = "Student.txt"; 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("--------------------------------------------------------------"); System.out.print("请输入您的选择:"); Scanner sc = new Scanner(System.in); String choice = sc.nextLine(); switch (choice) { case "1": //查看所有学生 findAllStudent(fileName); System.out.println(); break; case "2": //添加学生 addStudent(fileName); System.out.println(); break; case "3": deleteStudent(fileName); System.out.println(); break; case "4": updateStudent(fileName); System.out.println(); break; case "5": default: // System.out.println("------ 5. 退出 -----"); System.out.println(" 感谢您的使用 "); //flag = false; System.exit(0); break; } } } //IO输入流:读文件 public static void readData(String fileName, ArrayList array) throws IOException { //创建输入缓冲流对象 BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while((line = br.readLine()) != null){ String[] datas = line.split(","); Student s = new Student(datas[0],datas[1],datas[2],datas[3]); array.add(s); } br.close(); } //IO输出流:写文件 public static void writeData(String fileName, ArrayList array) throws IOException { //创建输入缓冲流对象 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); for(int x = 0; x ArrayList array = new ArrayList(); readData(fileName,array); if(array.size() == 0){ System.out.println("无学生信息,请重新选择"); return; } System.out.println(); System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄\t\t\t\t居住地"); System.out.println("-------------------------------------------------------------------------"); for(int x = 0; x ArrayList array = new ArrayList(); readData(fileName,array); Scanner in = new Scanner(System.in); String id =""; while(true) { System.out.print("输入学号:"); id = in.nextLine(); int x; boolean flag = false; for(x = 0; x flag = true; break; } } if(flag){ System.out.println("学号被占用,请重新输入"); } else{ break; } } System.out.print("输入姓名:"); String name = in.nextLine(); System.out.print("输入年龄:"); String age = in.nextLine(); System.out.print("输入地址:"); String address = in.nextLine(); //创建学生对象 Student s = new Student(id,name,age,address); /* Student s = new Student(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); */ array.add(s); writeData(fileName, array); System.out.println("添加学生成功"); } //删除学生 public static void deleteStudent(String fileName) throws IOException { ArrayList array = new ArrayList(); readData(fileName,array); //思路:根据学号删除。先遍历学生列表查看学生是否存在 Scanner in = new Scanner(System.in); System.out.print("输入要删除学生的学号:"); String id = in.nextLine(); int index = -1; for(int x = 0; x index = x; break; } } if(index == -1){ System.out.print("该学生信息不存在,是否继续当前删除操作(Yes or No):"); String choice = in.nextLine(); if(choice.equalsIgnoreCase("Yes")){ deleteStudent(fileName); }else if(choice.equalsIgnoreCase("No")) { return; } else{ System.out.println("输入信息有误,返回主界面"); return; } }else { array.remove(index); writeData(fileName,array); System.out.println("删除学生成功"); } } //修改学生信息 public static void updateStudent(String fileName) throws IOException { ArrayList array = new ArrayList(); readData(fileName,array); //输入学号查找学生是否存在 Scanner in = new Scanner(System.in); System.out.print("输入要更新学生的学号:"); String id = in.nextLine(); int index = -1; for(int x = 0; x index = x; break; } } if(index == -1){ System.out.print("该学生信息不存在,是否继续当前更新操作(Yes or No):"); String choice = in.nextLine(); if(choice.equalsIgnoreCase("Yes")){ updateStudent(fileName); }else if(choice.equalsIgnoreCase("No")) { return; } else{ System.out.println("输入信息有误,返回主界面"); return; } }else { Student s = array.get(index); boolean flag = true; System.out.println("-----选择更新学生信息选项------"); System.out.println("-- 1. 姓名 --"); System.out.println("-- 2. 年龄 --"); System.out.println("-- 3. 地址 --"); System.out.println("-- 4. 返回主界面 --"); System.out.println("--------------------------"); while(flag) { System.out.print("请输入您的选择:"); Scanner sc = new Scanner(System.in); String choice = sc.nextLine(); switch (choice) { case "1": System.out.print("输入修改姓名:"); String name = in.nextLine(); s.setName(name); System.out.print("继续更新操作(Yes or No):"); String a = in.nextLine(); if(a.equalsIgnoreCase("Yes")) break; else if(a.equalsIgnoreCase("No")){ array.set(index,s); writeData(fileName,array); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "2": System.out.print("输入修改年龄:"); String age = in.nextLine(); s.setAge(age); System.out.print("继续更新操作(Yes or No):"); String b = in.nextLine(); if(b.equalsIgnoreCase("Yes")) break; else if(b.equalsIgnoreCase("No")){ array.set(index,s); writeData(fileName,array); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "3": System.out.print("输入修改地址:"); String address = in.nextLine(); s.setAddress(address); System.out.print("继续更新操作(Yes or No):"); String c = in.nextLine(); if(c.equalsIgnoreCase("Yes")) break; else if(c.equalsIgnoreCase("No")){ array.set(index,s); writeData(fileName,array); System.out.println("更新操作完成,返回主界面"); return; } else{ System.out.println("输入信息有误,返回主界面"); return; } case "4": array.set(index,s); writeData(fileName,array); System.out.println("修改信息完成。"); return; default: System.out.println("输入信息有误。返回主界面"); return; } } } } }

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

在这里插入图片描述

在这里插入图片描述 在这里插入图片描述

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有