本文章主要利用数组存储来实现对学生信息的增删改查以及成绩排序等,以下为简单的学生管理代码(无界面)
package Student.one;
public class Stu {
String name;
String id;
String gender;
int score;
int age;
public Stu() {
super();
}
public void setid(String id) {
this.id = id;
}
public void setname(String name) {
this.name = name;
}
public void setgender(String gender) {
this.gender = gender;
}
public void setscore(int score)
{
this.score = score;
}
public void setage(int age) {
this.age = age;
}
public String getname() {
return name;
}
public int getscore() {
return score;
}
public String getgender() {
return gender;
}
public String getid() {
return id;
}
public int getage() {
return age;
}
public String toString() {
return id+" "+name+" " +gender+"\t"+age+"\t"+score;
}
}
package Student.one;
import java.util.*;
public class StuMage {
static Stu[] stu1 = new Stu[100];
static int index=0;
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
int choice;
System.out.println("————————这是一个学生管理系统————————");
while(true)
{
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("请输入您需要的选项:");
choice = in.nextInt() ;
if(choice == 6) break;
switch(choice) {
case 1: add() ;
break;
case 2: delete() ;
break;
case 3: change() ;
break;
case 4: view();
break;
case 5: Sort();
}
}
}
static void Sort() {
Stu temp = new Stu();
int j;
for(int i=0;i<index;i++) {
for(j=i+1;j<index;j++) {
if(stu1[i].getscore()<stu1[j].getscore())
{
temp = stu1[i];
stu1[i] = stu1[j];
stu1[j] = temp;
}
}
System.out.println("排序完成,选择”学生信息查看“进行查看");
}
}
static void add() {
while(true) {
System.out.println("请按提示输入以下信息:");
stu1[index] = new Stu();
System.out.print("请输入学生学号:");
stu1[index].id = in.next();
System.out.println("请输入学生姓名:");
stu1[index].name = in.next();
System.out.println("请输入学生年龄:");
stu1[index].age = in.nextInt() ;
System.out.println("请输入学生性别:");
stu1[index].gender = in.next();
System.out.println("请输入学生分数:");
stu1[index].score = in.nextInt() ;
index++;
System.out.println("添加完成");
System.out.print("是否继续?退出输入0:");
if(in.nextInt()==0) break;
}
}
static void view() {
System.out.println("—————————信息查看——————————");
System.out.println(" 学号 姓名 性别 年龄 成绩");
for(int i=0;i<index;i++) {
System.out.println(stu1[i].toString());
}
System.out.println(index);
}
static void delete() {
System.out.println("请输入要删除的学生学号");
String number = in.next() ;
int i;
for(i=0;i<index;i++) {
if(number.equals(stu1[i].getid()))
break;
if(i==index)
System.out.println("无此学号!");
}
for(int j=i;j<index-1;j++) {
stu1[j]=stu1[j+1];
index--;
}
}
static void change(){
System.out.println("请输入要修改信息的学生学号");
String Stunumber=in.next();
for(int i=0;i<index;i++)
{
if(Stunumber.equals(stu1[i].getid()))
{System.out.println("请输入要修改的学生的信息,学号,姓名,性别,年龄,分数");
stu1[i].setid(in.next());
stu1[i].setname(in.next());
stu1[i].setgender(in.next());
stu1[i].setage(in.nextInt());
stu1[i].setscore(in.nextInt());
break;
}
if(index==i)System.out.println("对不起查无此人");
}
}
}
这里在加上一个以ArrayList作为存储结构的代码,大致相同,可以学习两者的有点和差别:
package StudentJavaSE;
public class Student {
// 学号
private String id;
// 姓名
private String name;
// 分数
private String score;
public Student() {
}
public Student(String id, String name, String score)
{
this.id = id;
this.name = name;
this.score = score;
}
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 getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
//把Student类 ,StudentManagement保存,方便的调用。
package StudentJavaSE;
//通过 Scanner 类来获取用户的输入
import java.util.Scanner;
//创一个列表,用来存放其它java对象的,只要是java对象就可以往ArrayList里面放
import java.util.ArrayList;
//Collections类提供了一些操作集合的方法
import java.util.Collections;
//利用comparator能实现排序功能
import java.util.Comparator;
public class StudentAdmin {
//主方法
public static void main(String[] args) {
// 创建集合对象,用于存储学生数据
ArrayList<Student> list = new ArrayList<Student>();
// 使用循环让程序回到此处
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
Scanner sc = new Scanner(System.in);
//snextLine()扫描此行输入所有字符串直到遇到回车为止
String choiceString = sc.nextLine();
// 用switch语句实现选择
switch (choiceString) {
case "1":
// 调用查看所有学生的函数
findAllStudent(list);
break;
case "2":
// 添加学生
addStudent(list);
break;
case "3":
// 删除学生
deleteStudent(list);
break;
case "4":
// 修改学生
updateStudent(list);
break;
case "5":
// 按照分数排序查询
orderByScore(list);
break;
case "6":
//退出
System.out.println("谢谢你的使用");
break;
default:
System.out.println("你乱按了键盘莫得这个功能");
// System.exit(0); // JVM退出
break;
}
}
}
/*
* 修改学生的方法 修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生
*/
public static void updateStudent(ArrayList<Student> list) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:");
String id = sc.nextLine();
// 定义一个索引
int index = -1;
// 遍历集合
for (int x = 0; x < list.size(); x++) {
// 获取每一个学生对象
Student s = list.get(x);
// 拿学生对象的学号和键盘录入的学号进行比较
if (s.getId().equals(id)) {
index = x;
break;
}
}
if (index == -1) {
System.out.println("你要修改的学号对应的学生信息不存在,请回去重新你的选择");
}
else {
System.out.println("请输入学生新姓名:");
String name = sc.nextLine();
System.out.println("请输入学生新成绩:");
String score = sc.nextLine();
// 创建学生对象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setScore(score);
// 修改集合中的学生对象
list.set(index, s);
// 给出提示
System.out.println("修改学生成功");
}
}
/*
* 删除学生的方法 删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生
*/
public static void deleteStudent(ArrayList<Student> list) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生的学号:");
String id = sc.nextLine();
// 我们必须给出学号不存在的时候的提示
// 定义一个索引
int index = -1;
// 遍历集合
for (int x = 0; x < list.size(); x++) {
// 获取到每一个学生对象
Student s = list.get(x);
// 拿这个学生对象的学号和键盘录入的学号进行比较
if (s.getId().equals(id)) {
index = x;
break;
}
}
if (index == -1) {
System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新你的选择");
} else {
list.remove(index);
System.out.println("删除学生成功");
}
}
// 添加学生的方法
public static void addStudent(ArrayList<Student> list) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
// 为了让id能够被访问到,我们就把id定义在了循环的外面
String id;
// 为了让代码能够回到这里,用循环
while (true) {
System.out.println("请输入学生学号:");
// String id = sc.nextLine();
id = sc.nextLine();
// 判断学号有没有被人占用
// 定义标记
boolean flag = false;
// 遍历集合,得到每一个学生
for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
// 获取该学生的学号,和键盘录入的学号进行比较
if (s.getId().equals(id)) {
flag = true; // 说明学号被占用了
break;
}
}
if (flag) {
System.out.println("你输入的学号已经被占用,请重新输入");
}
else {
break; // 结束循环
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生成绩:");
String score = sc.nextLine();
// 创建学生对象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setScore(score);
// 把学生对象作为元素添加到集合
list.add(s);
// 给出提示
System.out.println("添加学生成功");
}
// 查看所有学生
public static void findAllStudent(ArrayList<Student> list)
{
// 首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行
if (list.size() == 0) {
System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
return;
}
// \t 其实就是一个tab键的位置
System.out.println("学号\t\t姓名\t成绩");
for (int x = 0; x < list.size(); x++)
{
Student s = list.get(x);
System.out.println(s.getId() + "\t"+ "\t"+s.getName() + "\t" +"\t"+ s.getScore() );
}
}
// 根据分数排序
private static void orderByScore(ArrayList<Student> list ) {
if (list.size() <= 0) {
System.out.println("数据库为空!!");
return;
}
// 克隆副本进行排序,避免对原始数据更改
ArrayList<Student> cList = (ArrayList<Student>) list.clone();
// 排序按照分数
Collections.sort(cList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
Double id1 = new Double(s1.getScore());
Double id2 = new Double(s2.getScore());
Integer result = id1 < id2 ? 1 : id1 > id2 ? -1 : 0;
return result;
}
});
// 打印输出分数
System.out.println("学号\t\t姓名\t成绩");
for (int i = 0; i < cList.size(); i++) {
Student s = cList.get(i);
System.out.println(s.getId() + "\t" + "\t" + s.getName() + "\t" + "\t" + s.getScore());
}
}}
这里我找到了我之前写的C语言学生信息管理,只找到了个简单的,之前的课设找不到了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct students
{
char name[20];
char num[10];
double mathscore;
double English;
double major;
int age;
} people;
void menu()
{
printf("----------------------------欢迎登陆学生管理系统--------------------------------\n");
printf("\n\t1.添加学生信息 2.显示某个学生信息 3.删除某个学生信息\n");
printf("\t4.显示所有学生信息 5.删除所有学生信息 6. 修改学生信息 \n");
printf("\t7. 8.修改背景颜色 0.退出系统\n ");
printf("\n-------------------------------------------------------------------------\n");
printf("选择数字 0-9******************************************************************\n");
};
void add()
{
struct students people;
FILE * fp;
char ch;
if ((fp=fopen("学生信息.txt","a+"))==NULL)
{
printf("\ncan not open the file \n");
getch();
exit(1);
}
do
{
printf("\t请输入学生名字:");
getchar();
gets(people.name);
printf("\t请输入学生学号:");
gets(people.num);
printf("\t请输入学生年龄:");
scanf("%d",&people.age);//年龄范围注意;
printf("\t专业成绩:");
scanf("%lf",&people.major);
printf("\t高数成绩:");
scanf("%lf",&people.mathscore);
printf("\t英语成绩:");
scanf("%lf",&people.English);
fwrite(&people,sizeof(people),1,fp);
printf("\tAny more?(Y/N):");
scanf("%s",&ch);
}
while (ch=='Y'||ch=='y');
printf("添加成功!!!\n");
fclose(fp);
}
void printf1()
{
printf("%-10s%-15s%-10s%-10s%-10s%-10s",
"学生姓名","学生学号","学生年龄","专业成绩","高数成绩","英语成绩");
printf("\n");
}
void printf2(struct students people)
{
printf("\n");
printf("%-10s%-15s",people.name,people.num);
printf("%-10d",people.age);
printf("%-10.1lf%-10.1lf%-10.1lf\n",people.major,people.mathscore,people.English);
}
void display()
{
struct students people;
FILE *fp;
char ch[100];
int total;
char f;
if ((fp=fopen("学生信息.txt","r"))==NULL)
{
printf("\ncan not open the file!\n");
getch();
exit(1);
}
do
{
rewind(fp);
printf("\n请输入查询学生的学号:");
scanf("%s",ch);
total=0;
while (fread(&people,sizeof(people),1,fp)==1)
{
if(strcmp(ch,people.num)==0)
{
total++;
if(total==1)
{
printf1();
}
printf2(people);
}
}
printf("\nsearch any more?(Y/N):\n");
scanf("%s",&f);
}
while(f=='Y'||f=='y');
fclose(fp);
}
void delete1()
{
struct students people;
FILE *fp,*fp1;
int flag;
char ch[100];
char f;
//char temp;
do
{
if((fp=fopen("学生信息.txt","rb"))==NULL)
{
printf("\tcan not open the file\n");
getch();
exit(1);
}
if((fp1=fopen("temp.txt","wb"))==NULL)
{
printf("\tcan not creat the temp file !");
getch();
exit(1);
}
printf("\t选取删除学生的学号:");
getchar();
gets(ch);
flag=0;
while(fread(&people,sizeof(people),1,fp)==1)
{
if(strcmp(ch,people.num)==0)
{
printf1();
printf2(people);
flag=1;
}else if(strcmp(ch,people.num)!=0)
fwrite(&people,sizeof(people),1,fp1);
}
fclose(fp);
fclose(fp1);
if(flag==1)
{
printf("\n删除成功!\n");
remove("学生信息.txt");
rename("temp.txt","学生信息.txt");
}
else
printf("can not find\n");
printf("是否继续删除?(Y/N):\n");
scanf("%s",&f);
}
while(f=='y'||f=='Y');
}
void show()
{
struct students people;
FILE *fp;
if((fp=fopen("学生信息.txt","rb"))==NULL)
{
printf("\tcan not open file!\n");
getch();
exit(1);
}
printf1();
while(fread(&people,sizeof(people),1,fp)==1)
printf2(people);
fclose(fp);
}
void change()
{
struct XX
{
char name[20];
char num[10];
double mathscore;
double English;
double major;
int age;
}xx;
FILE *fp,*fp1;
int flag;
char ch[30];
char f;
//char temp[20];
do
{
if((fp=fopen("学生信息.txt","rb"))==NULL)
{
printf("\tcan not open the file !\n");
getch();
exit(1);
}
if((fp1=fopen("temp.txt","wb"))==NULL)
{
printf("\tcon not open the file!\n");
getch();
exit(1);
}
printf("\t请输入想要修改的学号:");
getchar();
gets(ch);
flag=0;
while(fread(&people,sizeof(people),1,fp)==1)
{
if(strcmp(ch,people.num)==0)
{
printf1();
printf2(people);
printf("\n\n准备输入学生新的信息:\n");
printf("\t名字:");
gets(xx.name);
printf("\t学号:");
gets(xx.num);
printf("\t年龄:");
scanf("%d",&xx.age);
printf("\t专业成绩:");
scanf("%lf",&xx.major);
printf("\t高数成绩:");
scanf("%lf",&xx.English);
printf("\t英语成绩:");
scanf("lf",&xx.English);
flag=1;
fwrite(&xx,sizeof(xx),1,fp1);
}
else if(strcmp(ch,people.num)!=0)
{
fwrite(&people,sizeof(people),1,fp1);
}
}
fclose(fp);
fclose(fp1);
if(flag==1)
{
printf("修改成功!\n");
remove ("学生信息.txt");
rename("temp.txt","学生信息.txt");
}
else
printf("can not find this record!\n");
printf("还要修改吗?(Y/N):[]\b\b");
f=getchar();
getchar();
}
while(f=='Y'||f=='y');
}
//void deleteall()
//{
// FILE *fp,*fp1;
// if((fp=fopen("学生信息.txt","rb"))==NULL)
// {
// printf("\t can not open file!\n");
// getch();
// exit(1);
//
// }
// if((fp1=fopen("temp.txt","wb"))==NULL)
// {
// printf("\t can not open file !\n");
// getch();
// exit(1);
// }
//}
int main()
{
int a;
do
{
menu();
scanf("%d",&a);
switch(a)
{
case 1:
add();
printf("~~~~~~~~~~~~~~~~~~~~~~~~按任意键跳出主页面!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~");
getch();
break;
case 2:
display();
break;
case 3:
delete1();
system("cls");
break;
case 4:
show();
printf("--------------------------按任意键继续!!!!!--------------------------");
getch();
break;
system("cls");
break;
case 6:
change();
break;
}
}
while("a!=0");
system("cls");
return 0;
}