Java实现学生简易信息管理系统
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现学生简易信息管理系统相关的知识,希望对你有一定的参考价值。
写一个简易的学生管理系统,要求如下:
1.使用list接口
2.实现增加学生信息,要求不能重复添加相同学号的学生(类名add)
3.实现根据学号删除学生信息(类名remove)
4.实现根据学号查找学生信息(类名find)
5.实现根据学号更改对应学生信息,要求修改之后不能是已有学生(类名modify)
6.遍历学生信息
7.每个功能拆分为单个类
8.根据用户输入的信息使用I/O流存放在txt文件中
9.实现类功能:根据用户输入的数字,进入对应功能,如:1.增加信息,2.删除信息
10.学生信息有:学号(int id),姓名(String name),年龄(int age)
这道编程题知道一些眉目,但却没法用代码详细写出,求大神帮助!
感激不尽!
import java.io.*;
class StuMgr
public static class Student
public int id;
public String name;
public int age;
public Student(int id ,String name,int age)
this.id = id;
this.name = name;
this.age = age;
@Override
public String toString()
return id + "," + name + "," + age;
public List<Student> stuList = new LinkedList<>();
public void add()
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try
intId = Integer.parseInt(id);
catch(NumberFormatException ex)
System.out.println("学号输入有误,请输入数字!");
return;
if (find(intId) != null)
System.out.println("该学号已经存在!");
return ;
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
int intAge = 0;
try
intAge = Integer.parseInt(age);
catch(NumberFormatException ex)
System.out.println("年龄输入有误,请输入数字!");
return;
Student stu = new Student(intId,name,intAge);
stuList.add(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已增加");
System.out.println(stu);
System.out.println("-----------------------");
public void del()
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try
intId = Integer.parseInt(id);
catch(NumberFormatException ex)
System.out.println("学号输入有误,请输入数字!");
return;
Student stu = find(intId);
if ( stu == null)
System.out.println("该学号不存在!");
return ;
stuList.remove(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已删除");
System.out.println(stu);
System.out.println("-----------------------");
public void find()
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try
intId = Integer.parseInt(id);
catch(NumberFormatException ex)
System.out.println("学号输入有误,请输入数字!");
return;
Student stu = find(intId);
if ( stu == null)
System.out.println("该学号不存在!");
return ;
System.out.println("-----------------------");
System.out.println("查找学生信息如下");
System.out.println(stu);
System.out.println("-----------------------");
public Student find(int id)
for(Student stu : stuList)
if(stu.id == id)
return stu;
return null;
public void modify()
store();
public void foreach()
System.out.println("-----------------------");
for(Student stu : stuList)
System.out.println(stu);
System.out.println("-----------------------");
public void store()
Iterator iterator = stuList.iterator();
File file = new File("stuList.txt");
FileWriter fw = null;
BufferedWriter writer = null;
try
fw = new FileWriter(file);
writer = new BufferedWriter(fw);
while(iterator.hasNext())
writer.write(iterator.next().toString());
writer.newLine();//换行
writer.flush();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
try
writer.close();
fw.close();
catch (IOException e)
e.printStackTrace();
public static void main(String[] args)
StuMgr mgr = new StuMgr();
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("6:退出");
System.out.println("-----------------------");
Scanner sc = new Scanner(System.in);
String op = sc.nextLine();
if("6".equals(op))
return;
if("1".equals(op))
mgr.add();
if("2".equals(op))
mgr.del();
if("3".equals(op))
mgr.find();
if("4".equals(op))
mgr.modify();
if("5".equals(op))
mgr.foreach();
时间仓促,还有一个modify方法没实现,留给你自己练手。
参考技术A 如果是你一个人开发,那就照着需求一步步做呗。比如: 首先要有登录界面,登录界面设计好,需要传入的参数有 用户名,密码,登录身份;这时你就设计一个数据库表 user(login_name,login_password,login_type);这时候登录进去,因为不同人的权限... 参考技术B不知道你要的是不是这个意思,随便写了点,你运行下,是这样就接着写就好了 ,很简单的.我就写了个新增的功能.保存到了你的e盘user.txt文件了
import java.io.File;import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class HasStatic
public static FileOutputStream fos;
public static void main(String[] args) throws IOException
Scanner sc = new Scanner(System.in);
//初始化
init();
System.out.println("请输入对应功能:1.增加信息,2.删除信息,3.查询信息");
int order = sc.nextInt();
switch(order)
case 1 :
addUser();
break;
case 2:
break;
case 3:
break;
fos.close();
public static void init() throws IOException
File file = new File("E://user.txt");
fos = new FileOutputStream(file);
if(!file.exists())
file.createNewFile();
String title = "学号 | 姓名 | 年龄\\r\\n";
fos.write(title.getBytes());
public static void addUser() throws IOException
int id ; //学号
String name ;//姓名
int age;//年龄
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
id = sc.nextInt();
System.out.println("请输入学生姓名:");
name = sc.next();
System.out.println("请输入学生年龄:");
age = sc.nextInt();
System.out.println("新增学生信息:");
System.out.println("学号:"+id);
System.out.println("姓名:"+name);
System.out.println("年龄"+age);
System.out.println("是否确定保存:1.确定 2.取消并退出");
int order = sc.nextInt();
switch(order)
case 1 :
saveUser(id,name,age);
break;
case 2:
break;
public static void saveUser(int id,String name,int age) throws IOException
//获取输入流
String idStr = id+" | ";
name = name + " | ";
String ageStr = age+"\\r\\n";
fos.write(idStr.getBytes());
fos.write(name.getBytes());
fos.write(ageStr.getBytes());
参考技术C 这里有很多的源代码,都是Java源码开发
node.js运行简易学生信息管理系统
const readline = require(‘readline-sync‘);
//学生信息
let users = [{
id: 1,
name: ‘liu‘,
age: ‘17‘,
gender: ‘male‘,
score: ‘98‘
},
{
id: 2,
name: ‘dog‘,
age: ‘18‘,
gender: ‘male‘,
score: ‘81‘
},
{
id: 3,
name: ‘xingxing‘,
age: ‘20‘,
gender: ‘male‘,
score: ‘79‘
},
{
id: 4,
name: ‘coco‘,
age: ‘16‘,
gender: ‘female‘,
score: ‘73‘
},
{
id: 5,
name: ‘along‘,
age: ‘15‘,
gender: ‘female‘,
score: ‘66‘
}
];
//账号密码
let admin = [{
username: ‘1‘,
password: ‘1‘
}]
//登录
function islogin(admin) {
let num = 3;
while (true) {
console.log(‘请输入帐号:‘);
let username = readline.question();
console.log(‘请输入密码:‘);
let password = readline.question();
for (let i = 0; i < admin.length; i++) {
if (admin[i].username == username && admin[i].password == password) {
console.log(‘登录成功‘);
noteMenu();
return;
}
}
num--;
if (num <= 0) {
console.log(‘错误3次,锁卡‘);
return num;
} else
console.log(`登录失败,你还有${num}次机会,请重新输入`);
}
}
//注册
function isregister(admin) {
while (true) {
console.log(‘请输入帐号:‘);
let username = readline.question();
console.log(‘请输入密码:‘);
let password = readline.question();
let isOk = false;
for (let i = 0; i < admin.length; i++) {
if (admin[i].username == username) {
console.log(‘用户名已注册,请重新输入‘);
isOk = true;
break;
}
}
if (!isOk) {
admin.push({
username,
password,
});
console.log(‘注册成功‘)
menu(admin);
return;
}
}
}
//主菜单
function menu(admin) {
while (true) {
console.log(‘1.登录 2.注册 3.切换用户‘)
console.log(‘请选择要进行的操作:‘)
let option = readline.question() - 0;
switch (option) {
case 1:
let num = islogin(admin);
if (num <= 0) {
return;
}
break;
case 2:
isregister(admin);
break;
case 3:
islogin(admin);
break;
default:
console.log(‘请输入正确选项‘);
}
}
}
//操作菜单
function noteMenu() {
while (true) {
console.log(‘请选择操作:1.查询 2.新增 3.修改 4.删除 5.退出‘)
let option = readline.question() - 0;
switch (option) {
case 1:
find();
break;
case 2:
addUsers();
break;
case 3:
change();
break;
case 4:
deletSth();
break;
case 5:
return;
default:
console.log(‘请输入正确的选项‘);
}
}
}
//查询
function find() {
console.log(‘请输入查询方式: 1.查询所有 2.分类查询‘);
let option = readline.question() - 0;
switch (option) {
case 1:
findAll();
break;
case 2:
splitClass();
break;
default:
console.log(‘请输入正确的选项‘);
}
}
//查询所有
function findAll() {
for (let i = 0; i < users.length; i++) {
console.log(users[i]);
}
}
//分类查询
function splitClass() {
console.log(‘请选择操作:1.姓名 2.年龄 3.性别 4.分数‘);
let option = readline.question() - 0;
switch (option) {
case 1:
allName();
break;
case 2:
userAge();
break;
case 3:
sex();
break;
case 4:
scoreAll();
break;
default:
console.log(‘请输入正确的选项‘);
}
}
//姓名查询
function allName() {
console.log(‘请输入要查询的名字‘);
let name = readline.question();
let isOk = false;
for (let i = 0; i < users.length; i++) {
if ((users[i].name).includes(name)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log(‘没有找到该学生‘);
}
}
//年龄查询
function userAge() {
while (true) {
console.log(‘请输入要查询的年龄‘);
let age = readline.question();
let reg = /^\d{1,3}$/;
let isOk = false;
if (reg.test(age) == true) {
for (let i = 0; i < users.length; i++) {
if ((users[i].age).includes(age)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log(‘没有找到包含该年龄的学生‘);
return;
} else
return;
} else {
console.log(‘错误,请输入正确的年龄‘);
}
}
}
//性别查询
function sex() {
console.log(‘请选择要查询的性别: 1.男 2.女‘);
let sel = readline.question() - 0;
let man = /^male$/,
woman = /^female$/;
switch (sel) {
case 1:
sexsel(man);
break;
case 2:
sexsel(woman);
break;
}
}
//性别筛选
function sexsel(sex) {
for (let i = 0; i < users.length; i++) {
if (sex.test(users[i].gender)) {
console.log(users[i]);
}
}
}
//分数查询
function scoreAll() {
console.log(‘请输入要查询的分数‘);
let score = readline.question();
let isOk = false;
for (let i = 0; i < users.length; i++) {
if ((users[i].score).includes(score)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log(‘没有找到包含该成绩的学生‘);
}
}
//新增
function addUsers() {
while (true) {
console.log(‘请输入新增学生姓名:‘);
let name = readline.question();
console.log(‘请输入新增学生年龄:‘);
let age = readline.question();
console.log(‘请输入新增学生性别:‘);
let gender = readline.question();
console.log(‘请输入新增学生分数:‘);
let score = readline.question();
let regName = /^[a-zA-Z]+$/; //限制名字只能由字母组成
let regAge = /^\d{1,2}$/; //限制年龄只能由数字组成
let regGender = /^female|male$/; //限制性别只能由male和female组成
let regScore = /^((\d{1,2})|(1[0-4]\d)|(150))$/; //限制成绩只能0-150
if (regName.test(name)) {
if (regAge.test(age)) {
if (regGender.test(gender)) {
if (regScore.test(score)) {
users.push({
id: `${users.length+1}`,
name,
age,
gender,
score
})
console.log(‘添加成功,该学生信息如下:‘);
console.log(users.pop());
return;
} else
console.log(‘成绩只能在0-150,请重新输入‘);
} else
console.log(‘性别只能是male或female,请重新输入‘);
} else
console.log(‘年龄只能由数字组成,请重新输入‘);
} else
console.log(‘名字只能由字母组成,请重新输入‘);
}
}
//修改
function change() {
console.log(‘请输入要修改的学生学号:‘);
let stuNum = readline.question() - 0;
for (let i = 0; i < users.length; i++) {
if (users[i].id === stuNum) {
console.log(‘当前学生信息如下:‘);
console.log(users[i]);
changeAll(i);
return;
}
}
console.log(‘没有该学号的学生‘);
}
//修改主菜单
function changeAll(i) {
console.log(‘请选择要修改的信息:1.姓名 2.年龄 3.性别 4.成绩‘);
let option = readline.question() - 0;
switch (option) {
case 1:
stuName(i);
break;
case 2:
stuAge(i);
break;
case 3:
stuSex(i);
break;
case 4:
stuScore(i);
break;
default:
console.log(‘请输入正确的选项‘);
}
}
//修改姓名
function stuName(i) {
while (true) {
console.log(‘请输入要修改的姓名:‘);
let name = readline.question();
let reg = /^[a-zA-Z]+$/;
if (reg.test(name) == true) {
users[i].name = name;
console.log(‘修改成功!该学生信息如下:‘);
console.log(users[i]);
return;
} else
console.log(‘请输入正确的名字(字母)‘);
}
}
//修改年龄
function stuAge(i) {
while (true) {
console.log(‘请输入要修改的年龄:‘);
let age = readline.question();
let reg = /^\d{1,2}$/;
if (reg.test(age) == true) {
users[i].age = age;
console.log(‘修改成功!该学生信息如下:‘);
console.log(users[i]);
return;
} else
console.log(‘请输入正确的年龄数字(1-2位数)‘);
}
}
//修改性别
function stuSex(i) {
while (true) {
console.log(‘请输入要修改的性别:‘);
let gender = readline.question();
let reg = /^female|male$/;
if (reg.test(gender) == true) {
users[i].gender = gender;
console.log(‘修改成功!该学生信息如下:‘);
console.log(users[i]);
return;
} else
console.log(‘请输入正确的性别(male或famel)‘);
}
}
//修改成绩
function stuScore(i) {
while (true) {
console.log(‘请输入要修改的成绩:‘);
let score = readline.question();
let reg = /^((\d{1,2})|(1[0-4]\d)|(150))$/;
if (reg.test(score) == true) {
users[i].score = score;
console.log(‘修改成功!该学生信息如下:‘);
console.log(users[i]);
return;
} else
console.log(‘请输入正确的成绩(0-150)‘);
}
}
//删除
function deletSth() {
while (true) {
console.log(‘请输入要删除的学生学号:‘);
let stuNum = readline.question() - 0;
let reg = /^\d+$/;
if (reg.test(stuNum) == true) {
for (let i = 0; i < users.length; i++) {
if (users[i].id === stuNum) {
console.log(‘当前学生信息如下:‘);
console.log(users[i]);
deletMenu(i);
return;
}
}
console.log(‘没有找到该学号学生‘);
} else
console.log(‘请输入正确的学号(数字)‘);
}
}
//删除主菜单
function deletMenu(i) {
console.log(‘是否确定删除:‘);
console.log(‘1.是 2.否‘);
let option = readline.question() - 0;
switch (option) {
case 1:
deletTrue(i);
break;
case 2:
return;
}
}
//确定删除
function deletTrue(i) {
users.splice(i, 1);
console.log(‘删除成功!‘);
}
menu(admin);
以上是关于Java实现学生简易信息管理系统的主要内容,如果未能解决你的问题,请参考以下文章