?? stu.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 + -