⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stu.java

📁 实现学生成绩的添加
💻 JAVA
字号:
import java.io.*;

public class stu{
	
	public static int count=0;                //记录系统中实际存储的学生信息的记录数
	public static int temp;                   //临时存储输入的数据
	public static int temp1;                  //临时存储高等数学成绩
	public static int temp2;                  //临时存储大学英语成绩
	public static int temp3;                  //临时存储Java程序设计成绩
	public static int temp4;                  //临时存储操作系统成绩
	public static int temp5;                  //临时存储体育成绩
	
	public static String s;
	public static String ss;                  //临时存储学生姓名
	
	public static class student{      //学生类
		public int id;
		public String name;
		public int math_score;
		public int english_score;
		public int java_score;
		public int system_score;
		public int sport_score;
		public float average_score;
		public int rank;
	}
	
	public static student[] st=new student[100];   //分配100个存储空间存储学生成绩
	
	public static void main(String args[]){      //主函数
		for(int i=0; i<100; i++){
			st[i]=new student();
			st[i].id = -1;                          //初始化所有学生学号为-1
		}
		System.out.print("*******************************************************************************\n");
		System.out.print("***************************欢迎使用学生成绩管理系统****************************\n");
		choices();
		functions();
	}
	
	public static void choices(){               //列出功能选项
		  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("0----退出**");
      System.out.println("***********");
  }
  
  public static void functions(){           //选择功能
  	while(true){
  		System.out.print("\n");
  		System.out.print("请输入选项:");
  		try{
  			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  			s=br.readLine();
  			temp=Integer.parseInt(s);
  			break;
  		}catch(Exception e){
  			System.out.print("**输入有误,请输入(1,2,3,4,5,6,0)中的一个数!!!\n");
  		}
  	}
  	
  	switch(temp){
  		case 0:                                   break;
      case 1:add();     choices(); functions(); break;
      case 2:del();     choices(); functions(); break;
      case 3:mod();     choices(); functions(); break;
      case 4:find();    choices(); functions(); break;
      case 5:showall(); choices(); functions(); break;
      case 6:sort();    choices(); functions(); break;
      default:  System.out.println("**输入有误,请输入(1,2,3,4,5,6,0)中的一个数!!!\n");  functions(); break;
    }
  }
  
  public static void add(){         //添加
  	while(true){
  		try{
  			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  			
        System.out.print("请输入学号:    ");
        st[count].id=Integer.parseInt(br.readLine());  
                   
        System.out.print("请输入姓名:    ");
        s=br.readLine();
        st[count].name=s;   
            
        System.out.print("请输入高等数学成绩(0~100):     ");
        temp1=Integer.parseInt(br.readLine());
        if(temp1<0||temp1>100) throw new Exception();
        st[count].math_score=temp1;
        
        System.out.print("请输入大学英语成绩(0~100):     ");
        temp2=Integer.parseInt(br.readLine());
        if(temp2<0||temp2>100) throw new Exception();
        st[count].english_score=temp2;
        
        System.out.print("请输入Java程序设计成绩(0~100): ");
        temp3=Integer.parseInt(br.readLine());
        if(temp3<0||temp3>100) throw new Exception();
        st[count].java_score=temp3;
        
        System.out.print("请输入操作系统成绩(0~100):     ");
        temp4=Integer.parseInt(br.readLine());
        if(temp4<0||temp4>100) throw new Exception();
        st[count].system_score=temp4;
        
        System.out.print("请输入体育成绩(0~100):         ");
        temp5=Integer.parseInt(br.readLine());
        if(temp5<0||temp5>100) throw new Exception();
        st[count].sport_score=temp5;
        
        st[count].average_score=(float)((temp5+temp4+temp3+temp2+temp1)/5.0);       
        count++;
        System.out.print("是否继续输入?(y/n)"); 
         		
    		while(true){
    			s=br.readLine();
  			  if(!s.equals("y") && !s.equals("n")){
  			  	System.out.print("**输入有错,请重新输入:  ");
  			  }
  			  else  break;
  		  }
  		  if(s.equals("n"))  break;
  		}catch(Exception e){
  			 System.out.println("**输入数据格式错误异常,添加失败!!请重新添加记录!");
  		}
    }
  }
  
  public static void del(){        //删除
  	if(count==0){
  		System.out.println("**系统暂无记录!");
  	  return;
  	}
   		while(true){
   			try{
  		    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   			  System.out.print("请输入学号:  ");
    		  temp=Integer.parseInt(br.readLine());
    		  int i;
    	 	  for(i=0;i<100;i++){
   			  if(temp==st[i].id) break;
    		  }
    		  if(i==100){
    		  	System.out.println("**请重新输入,该学号不存在或已被删除!");
    		  	break;
    		  }
    		  else{
    		  	show(i);
  		  	  System.out.print("确认删除?(y/n)");
  		  	  while(true){
  			  	  s=br.readLine();
  	  	      if(!s.equals("y") && !s.equals("n")){
  			 	     	System.out.println("输入有错,请重新输入:  ");
  				    }
  				    else break;
  		      }
  		      if(s.equals("n")) break; 	
  		      else if(s.equals("y")){
  		       	st[i].id=-1;
  		       	count--;
  		      	System.out.println("**删除成功");
  		      	break;
  		      }
  	      }
        }catch(Exception e){
    	     System.out.println("**输入数据格式错误异常,请重新输入!");
    	   }
      }
  }
  
  public static void mod(){        //修改
  	if(count==0){
  		System.out.println("**系统暂无记录!");
  	  return;
  	}
  	while(true){
   			try{
  		    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   			  System.out.print("请输入学号:  ");
    		  temp=Integer.parseInt(br.readLine());
    		  int i;
    	 	  for(i=0;i<100;i++){     //看记录是否存在
   			  if(temp==st[i].id) break;
    		  }
    		  if(i==100){
  	  	    System.out.println("**无法修改,该学号不存在或已被删除!");
  	  	    break;
  	      }
    		  
    		  else{
    		  	show(i);
  		  	  System.out.print("确认修改?(y/n)");
  		  	  while(true){
  			  	  s=br.readLine();
  	  	      if(!s.equals("y") && !s.equals("n")){
  			 	     	System.out.println("**输入错误,请重新输入:  ");
  				    }
  				    else break;
  		      }
  		      if(s.equals("n")) break; 	
  		      else if(s.equals("y")){
  		      	
  		      	System.out.print("学号更改为(#表示不修改):            ");
  		       	s=br.readLine();
   		       	if(!s.equals("#"))	temp=Integer.parseInt(s); 
   		       	else         temp=st[i].id;  
   		       	   	
  		       	System.out.print("姓名更改为(#表示不修改):            ");
  		       	s=br.readLine();
  		       	if(!s.equals("#"))	ss=s;
  		       	else         ss=st[i].name;  		  
  		       	     	
  		       	System.out.print("高等数学成绩更改为(#表示不修改):    ");
  		       	s=br.readLine();
   		       	if(!s.equals("#")) {  temp1=Integer.parseInt(s); if(temp1<0||temp1>100) throw new Exception();}
   		       	else                 temp1=st[i].math_score;
   		       	
   		       	System.out.print("大学英语更改为(#表示不修改):        ");
  		       	s=br.readLine();
   		       	if(!s.equals("#")) {  temp2=Integer.parseInt(s); if(temp2<0||temp2>100) throw new Exception();}
   		       	else                 temp2=st[i].english_score;
   		       	
   		       	System.out.print("java程序设计成绩更改为(#表示不修改):");
  		       	s=br.readLine();
   		       	if(!s.equals("#")) {  temp3=Integer.parseInt(s); if(temp3<0||temp3>100) throw new Exception();}
   		       	else                 temp3=st[i].java_score;
   		       	
   		       	System.out.print("操作系统成绩更改为(#表示不修改):    ");
  		       	s=br.readLine();
   		       	if(!s.equals("#")) {  temp4=Integer.parseInt(s); if(temp4<0||temp4>100) throw new Exception();}
   		       	else                 temp4=st[i].system_score;
   		       	
   		       	System.out.print("体育成绩更改为(#表示不修改):        ");
  		       	s=br.readLine();
   		       	if(!s.equals("#")) {  temp5=Integer.parseInt(s); if(temp5<0||temp5>100) throw new Exception();}
   		       	else                 temp5=st[i].sport_score;
   		       	
   		       	st[i].id=temp;            //无异常时,集中一起修改
   		       	st[i].name=ss;
   		       	st[i].math_score=temp1;
   		       	st[i].english_score=temp2;
   		       	st[i].java_score=temp3;
   		       	st[i].system_score=temp4;
   		       	st[i].sport_score=temp5;
  		       	st[i].average_score=(float)((temp1+temp2+temp3+temp4+temp5)/5.0);
  		      	System.out.println("**修改成功!");
  		      	break;
  		      }
  	      } 	      
        }catch(Exception e){
    	     System.out.println("**数据格式错误异常,修改失败!!请重新修改记录!");
    	   }
    }
  	
  }
  
  public static void find(){       //查找
  	if(count==0){
  		System.out.println("**系统暂无记录!");
  	  return;
  	}
  	while(true){
   			try{
  		    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   			  System.out.print("请输入学号:  ");
    		  temp=Integer.parseInt(br.readLine());
    		  int i;
    	 	  for(i=0;i<100;i++){
   			  if(temp==st[i].id) break;
    		  }
    		  if(i==100){
    		  	System.out.println("**该学号不存在或已被删除!");
  	  	    break;
    		  }
    		  else{
    		  	show(i);
    		  	break;
    		  }
    		  
        }catch(Exception e){
    	     System.out.println("**输入数据格式错误异常,请重新查找!");
    	   }
    }
  	
  }
  
  public static void showall(){     //全部显示
  	if(count==0){
  		System.out.println("**系统暂无记录!");
  	  return;
  	}
  	System.out.println("-------------------------------------------------------------------------------");
  	System.out.println("学号     姓名   高等数学   大学英语   Java程序设计   操作系统   体育   平均成绩");
  	for(int i=0;i<100;i++){
  		if (st[i].id!=-1){
  			System.out.println(st[i].id+"   "+st[i].name+"   "+st[i].math_score+"         "+st[i].english_score+"            "+st[i].java_score+"            "+st[i].system_score+"        "+st[i].sport_score+"       "+st[i].average_score);
      }
    }
  }
  
  public static boolean show(int k){
  	if (st[k].id!=-1){
  		System.out.println("------------------------------------------------------------------------------");
  	  System.out.println("学号     姓名   高等数学   大学英语   Java程序设计   操作系统   体育   平均成绩");
  	  System.out.println(st[k].id+"   "+st[k].name+"   "+st[k].math_score+"         "+st[k].english_score+"            "+st[k].java_score+"            "+st[k].system_score+"        "+st[k].sport_score+"       "+st[k].average_score);
  	  return true;
  	}
  	else 
  	  return false;
  }
  
  public static void sort(){
  	if(count==0){
  		System.out.println("**系统暂无记录!");
  	  return;
  	}
  	student s[] = new student[count];
  	student sss = new student();
  	int i=0,j=0;
  	while(i<100){
  		if(st[i].id!=-1){
  			s[j]=new student();
  			s[j]=st[i];
  			i++;
  			j++;
  		}
  		else{
  			i++;
  		}
  	}
  	for(int k=0; k<j-1; k++){
  		int max=k;
  		for(int m=k+1; m<j; m++){
  			if(s[m].average_score>s[k].average_score)  max=m;
  		}
  		sss=s[k]; s[k]=s[max]; s[max]=sss;
  	}
  	System.out.println("------------------------------");
  	System.out.println("学号     姓名   平均成绩  名次");
  	for(int k=0;k<j;k++){
  		s[k].rank=k+1;
  		System.out.println(s[k].id+"   "+s[k].name+"   "+s[k].average_score+"      "+s[k].rank);
    }   
  	
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -