package doit_edu.day4.Student_System;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.String;
import java.util.Scanner;
/* 需求:
1.录入学生成绩
输入要录入的学生的数量
2.查看学生成绩
1.查看所有成绩
2.查看平均成绩
3.查看最高分和最低分
使用排序
4.查看及格率
3.查找学生排名阿和成绩
本质就是查找分数在数组中的索引 + 1
4.添加或者删除新的学生成绩
1.添加或者删除几个学生
2.添加新成绩*/
public class Students_System {
public static void main(String[] args) {
System.out.println("hello");
Student s = new Student();
s.Welcome_To_System();
}
}
class Student{
ArrayList<String> student_name;
ArrayList<Integer> student_store;
private boolean is_run = true;
private boolean is_sort = false;
//构造函数
Student(){
this.student_name = new ArrayList<String>();
this.student_store = new ArrayList<Integer>();
}
//支持导入数据库
Student(ArrayList<String> student_name,ArrayList<Integer> student_store){
this.student_store = student_store;
this.student_name = student_name;
}
//功能区-----------------------------------------
//自检程序
boolean Self_Safe(){
if (student_store.size()==student_name.size()){
return true;
}else{
return false;
}
}
//判空
boolean Is_Empty(){
if (student_name.isEmpty()||student_store.isEmpty()){
return true;
}else{
return false;
}
}
//存储程序
boolean Add_inside(String name,int store){
if (Self_Safe()){
this.student_name.add(name);
this.student_store.add(store);
return true;
}else{
System.out.println("存入失败");
return false;
}
}
//删除学生
boolean Del_Name_Store(int index){
if(Is_Empty()){
return false;
}else{
student_store.remove(index);
student_name.remove(index);
return true;
}
}
//使用插入排序对数据进行排序。
boolean Chiose_Sort(){
if (Is_Empty()){
System.out.println("数据库还没有数据,无法排序。请先输入一些数据在进行排序。");
return false;
}
for (int i = 1; i <student_store.size() ; i++) {
for (int j = i; j >0 ; j--) {
if(student_store.get(j)<student_store.get(j-1)){
//将分数数组先换顺序
int store_cache = student_store.get(j);
student_store.set(j,student_store.get(j-1));
student_store.set(j-1,store_cache);
//再将名字数组交换顺序
String name_cache = student_name.get(j);
student_name.set(j,student_name.get(j-1));
student_name.set(j-1,name_cache);
break;
}
}
}
this.is_sort = true;
return true;
}
//查找学生(只负责查询,查询到返回位置,查询不到返回-1)
int Search_Student(String need_search_name){
if (student_name.contains(need_search_name)){
return student_name.indexOf(need_search_name);
}else{
//数据库查询不到该同学,返回-1
return -1;
}
}
//功能区结束--- ------------------------ ------------
//程序启动菜单
void Welcome_To_System(){
Scanner sc = new Scanner(System.in);
while(is_run){
System.out.println("-----------------------------");
System.out.println("欢迎使用学生成绩管理系统!!!! ");
System.out.println("数据库中已有: "+student_name.size()+"对数据");
System.out.println("菜单: ");
System.out.println(" 1.添加并录入学生成绩 ");
System.out.println(" 2.查看学生成绩 ");
System.out.println(" 3.查找或修改学生排名和分数 ");
System.out.println(" 4.删除已有的学生信息 ");
System.out.println(" 5.退出程序 exit ");
System.out.println("输入对应的数字进行操作 ");
System.out.println("----------------------------- ");
switch(sc.next()){
case "1":
//增加学生信息
Add();
break;
case "2":
//查看学生成绩
View_Student_Store();
break;
case "3":
//查找已有的学生排名和成绩
Search_Student_and_Sore();
break;
case "4":
//删除已有的学生信息
Delete_Student();
break;
case "5":
//退出
this.is_run = false;
break;
}
}
}
//添加学生姓名和分数的主方法
void Add(){
System.out.println("请输入要添加的学生数量:");
Scanner sc = new Scanner(System.in);
int num_cache = sc.nextInt();
int[] student_cache_store = new int[num_cache];
String[] student_cache_name = new String[num_cache];
for (int i = 0; i < num_cache; i++) {
System.out.println("请输入第"+(i+1)+"学生姓名:");
student_cache_name[i] = sc.next();
System.out.println("请输入第"+(i+1)+"学生分数:");
student_cache_store[i] = sc.nextInt();
}
System.out.println("输入结束!本次输入:");
System.out.println("-------------------");
for (int i = 0; i <num_cache ; i++) {
System.out.println(student_cache_name[i]+" "+student_cache_store[i]);
}
System.out.println("-------------------");
//对比两个数组长度是否一致。
if(student_cache_name.length == student_cache_store.length ){
for (int i = 0; i < student_cache_name.length; i++) {
//调用添加函数进行存储操作。
Add_inside(student_cache_name[i],student_cache_store[i]);
System.out.println("传入数据成功!");
}
Chiose_Sort();
return;
}else{
System.out.println("乱几把输入,名字和分数不映射。");
return;
}
}
//查看学生成绩单
void View_Student_Store(){
if(Is_Empty()){
System.out.println("当前数据库中数据为空!");
return;
}
if(!this.is_sort){
Chiose_Sort();
}
System.out.println("-----------成绩排名-----------");
System.out.println("--姓名-- --分数--");
for (int i=0;i<student_store.size();i++){
System.out.println(i+1+". "+student_name.get(i)+" "+student_store.get(i));
}
System.out.println("-----------------------------");
return;
}
//计算平局值,取最大值和最小值,计算及格率
boolean View_Max_Min_Average(){
if(Is_Empty()){
System.out.println("当前数据库内没有内容,请输入一些数据再来查看。");
return false;
}else{
if(!this.is_sort){
Chiose_Sort();
}
int sum = 0;
double pr_cache = 0;
for (int i = 0; i <student_store.size() ; i++) {
sum += student_store.get(i);
if (student_store.get(i)>60){
pr_cache++;
}
}
System.out.println("平均值是:"+(sum/student_store.size()));
System.out.println("成绩最好的是:"+student_name.get(student_name.size()-1)+" "+student_store.get(student_store.size()-1));
System.out.println("成绩及格率为: "+((pr_cache/student_store.size())*100)+"%");
}
return true;
}
//查找学生成绩和排名
void Search_Student_and_Sore(){
if(Is_Empty()){
return;
}else{
Scanner sc = new Scanner(System.in);
//标识搜索运行状态
int run_seach_0= 1;
int run_seach_1 = 1;
while(run_seach_0 == 1){
System.out.println("开始查找学生,请输入姓名:");
String name_cache = sc.next();
int site = Search_Student(name_cache);
if (site < 0){
System.out.println("对不起,没有查找到该同学的信息!!!!");
System.out.println("是否继续?1/0/其它退出");
run_seach_1= sc.nextInt();
if (run_seach_1 == 0){
System.out.println("返回上一层");
run_seach_0 = 0;
}
else if(run_seach_1 == 1){
run_seach_0 = 1;
}else{
System.out.println("退出");
return;
}
}else{
System.out.println("---------------------------");
System.out.println("查找到该同学,列出该同学的信息:");
System.out.println("姓名:"+this.student_name.get(site));
System.out.println("分数:"+this.student_store.get(site));
System.out.println("排名:"+site+1);
System.out.println("---------------------------");
System.out.println("是否修改同学信息?1/0");
int s_1 = sc.nextInt();
if(s_1 == 1){
System.out.println("修改姓名输入1,修改分数输入2");
int num_cache = sc.nextInt();
if(num_cache==1){
System.out.println("请输入新名字:");
String new_name = sc.next();
student_name.set(site,new_name);
return;
}
else if(num_cache==2){
System.out.println("请输入新的分数:");
int new_store = sc.nextInt();
student_store.set(site,new_store);
return;
}else{
System.out.println("输入错误,再见!");
return;
}
}
System.out.println("是否继续查找?1/0");
run_seach_0 = sc.nextInt();
}
}
System.out.println("谢谢使用,再见!");
return;
}
}
boolean Delete_Student(){
System.out.println("-----------删除操作-------------");
Scanner sc = new Scanner(System.in);
if(Is_Empty()){
System.out.println("数据库是空的!!!!!!");
return false;
}
System.out.println("请输入要删除的学员姓名:");
String need_del_student_name = sc.next();
int need_del_student_site = Search_Student(need_del_student_name);
if (need_del_student_site<0){
System.out.println("您输入的学员不在数据库中,");
sc.next();
return false;
}
Del_Name_Store(need_del_student_site);
return true;
}
}
java实现学生管理系统
最新推荐文章于 2024-07-19 17:37:28 发布