package summerPractice;
import java.util.NoSuchElementException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
public class Home {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
//實驗文檔資料轉換
//确定學生人數
//out1和out2為操作是的試驗文本
System.out.println("先将資訊按:學号+名字+性别+高數+馬列+C語言+計技基礎的格式輸入到data/output.txt文檔中;輸入out 退出,輸入任意字元進入系統。");String enter=input.next();
//判斷是否進入系統
while(!enter.equals("out")){
CopyRight.CopyRight(1);//版權書寫
int studentNum=0;Scanner outS=new Scanner(new File("data/output.txt"));
PrintWriter out1=new PrintWriter(new File("data/out1.txt"));PrintWriter out2=new PrintWriter(new File("data/out2.txt"));
while(outS.hasNext()){
for(int i=0;i<3;i++){if(i!=2)out1.print(outS.next()+" "); else out1.println(outS.next());}
for(int i=0;i<5;i++){if(i!=4)out2.print(outS.nextInt()+" "); else out2.println(outS.nextInt());}
studentNum++;
}outS.close();out1.close();out2.close();
//System.out.println(studentNum);//檢驗學生人數
//将原始資料資訊和分數分别複制到student.txt和scores.txt文檔中
PrintWriter studentData=new PrintWriter(new File("data/student.txt"));
PrintWriter scoresData=new PrintWriter(new File("data/student1.txt"));
Scanner dataSource=new Scanner (new File("data/output.txt"));
while(dataSource.hasNext()){
for(int n=0;n<3;n++){if(n!=2)studentData.print(dataSource.next()+" ");else studentData.println(dataSource.next());}
for(int n=0;n<5;n++){if(n!=4)scoresData.print(dataSource.nextInt()+" ");else scoresData.println(dataSource.next());}
}studentData.close();scoresData.close();scoresData.close();
String[][] student=new String[studentNum][3];String[][] student1=new String[studentNum][3];
int[][] scores=new int[studentNum][5];
Scanner source=new Scanner(new File("data/student.txt"));
Scanner source1=new Scanner(new File("data/student1.txt"));
//基本資訊二維數組student;
for(int i=0;i
int n=0;while(source.hasNext()&&n<=2){
student [i][n]=source.next();String s=student[i][n];student1[i][n]=s;//student1為原始基本資訊數組
n++;
}
}source.close();
//成績二維數組scores;一行為個人高數。英語。馬列,C語言,計技基礎的成績
for(int i=0;i
int n=0;while(source1.hasNext()&&n<=4){
scores [i][n]=source1.nextInt();n++;
}
}source1.close();
//個人各科分數求和,輸入列,存入Sum數組中
double[] Sum=new double[studentNum];
for(int i=0;i
double sum=0;for(int n=0;n<5;n++){
sum=sum+scores [i][n];
}Sum[i]=sum;
}
//實作總分排名輸出和排名檢索: 将數組按總分排名寫出
for(int i=0;i
String[] turn1=new String[3];int[] turn2=new int[5];
double max=Sum[i];for(int n=i+1;n
if(Sum[n]>max){
max=Sum[n];Sum[n]=Sum[i];Sum[i]=max;//總分替換
turn1=student[i];student[i]=student[n];student[n]=turn1;//替換基本資訊
turn2=scores[i];scores[i]=scores[n];scores[n]=turn2;//替換各科分數數組
}
}
}//for(int i=0;i<10;i++){System.out.println(Sum[i]+student[i][1]);}總分列印檢驗,數組已經是按總分排名
System.out.println("共:"+studentNum+"名學生"+"輸入操作類型case:(0:基本資訊;1:修改資訊;2:總分排名列印;3:單科資料;4:總分排名查詢;5:基本資訊查詢;6:列印成績報表)");
int type=input.nextInt();
switch(type){
//顯示學生基本資訊
case 0:
for(int i=0;i
System.out.println(Arrays.toString(student1[i]));
}break;
case 1:
//修改資訊
System.out.println("資訊修改,輸入1增加個人資料,輸入2删除個人資料,輸入3更改已有人員資訊:");
int update=input.nextInt();PrintWriter output = new PrintWriter(new FileWriter("data/output.txt", true));
PrintWriter outputData = new PrintWriter(new FileWriter("data/testData.txt", true));
//增加檔案
if(update==1){
System.out.println("請輸入增加人:");
int i=input.nextInt();System.out.println("輸入學号+姓名+性别+高數+英語+馬列+c語言+計技基礎:");
for(int t=0;t
for(int n=0;n<3;n++){String s=input.next();output.print(s+" ");}
for(int n=0;n<5;n++){int num=input.nextInt();if(n!=4)output.print(num+ " ");else output.println(num);}
}
output.close();outputData.close();System.out.println("操作成功,資料儲存在data/output.txt文檔中");
}
//删除檔案
else if(update==2){
System.out.println("輸入個人資訊:學号或姓名:");java.io.File file=new java.io.File("data/output.txt");
String s=input.next();int count=-1;
for(int i=0;i
if(s.equals(student[i][0])||s.equals(student[i][1])){
count=i;
}
}
if(count>=0){System.out.println("即将删除的資訊為:"+Arrays.toString(student[count])+" "+Arrays.toString(scores[count])+"輸入yes删除,輸入其他則取消:");
String yes=input.next();
if(yes.equals("yes")){DelectAims.delectAims(file, s);
Scanner delSource=new Scanner (new File("data/delMid.txt"));PrintWriter delRusult=new PrintWriter(new File("data/output.txt"));
while(delSource.hasNextLine()){
delRusult.println(delSource.nextLine());
}delSource.close();delRusult.close();System.out.println("操作成功,資料儲存在原文檔data/output.txt中");
}
else System.out.println("取消删除成功!");
}
else System.out.println("沒有該學生資訊,請重新操作。");
}
//修改個人資訊
else {
System.out.println("輸入名字和修改科目(1-5):高數1;英語2;馬列3;c語言;計技基礎5和新的分數");String name=input.next();int subject=input.nextInt();int num=input.nextInt();
for(int i=0;i
if(student[i][1].equals(name)||student[i][0].equals(name)){
System.out.println();scores[i][subject-1]=num;System.out.println(scores[i][subject-1]);
}
}
PrintWriter editMid=new PrintWriter(new File("data/editMid.txt"));
for(int i=0;i
for(int n=0;n<3;n++){editMid.print(student[i][n]+" ");}//列印到文檔
for(int n=0;n<5;n++){if(n!=4)editMid.print(scores[i][n]+" ");else editMid.println(scores[i][n]);}
}editMid.close();//需要确定文檔名
Scanner editSource=new Scanner(new File("data/editMid.txt"));PrintWriter editResult=new PrintWriter(new File("data/output.txt"));
while(editSource.hasNextLine()){
editResult.println(editSource.nextLine());
}editSource.close();editResult.close();
System.out.println("操作成功,資料儲存在data/output.txt文檔中");
System.out.println(Arrays.toString(student[1])+" "+Arrays.toString(scores[1]));
}
break;
case 2:
//總分統計
java.io.PrintWriter sumData=new java.io.PrintWriter("data/總分統計.txt");
for(int i=0;i
sumData.print(Arrays.toString(student[i]));sumData.println(Sum[i]);
}sumData.close();System.out.println("資料儲存于data/總分統計.txt文檔中");
break;
case 3:
//單科最高分和最低分:儲存到SumSingle數組中。平均分儲存到SingleAverage數組中
double[] maxSingle=new double[5];double[] SingleAverage=new double[5];double[] minSingle=new double[5];
double sum1=0;double sum2=0;double sum3=0;double sum4=0;double sum5=0;double max=0;double min=0;
//單科平均分
for(int i=0;i
sum1+=scores[i][0];sum2+=scores[i][1];sum3+=scores[i][2];sum4+=scores[i][3];sum5+=scores[i][4];
}
SingleAverage[0]=(sum1*1.0)/studentNum;SingleAverage[1]=((sum2)*1.0)/studentNum;SingleAverage[2]=(sum3*1.0)/studentNum;
SingleAverage[3]=(sum4*1.0)/studentNum;SingleAverage[4]=(sum5*1.0)/studentNum;
//單科最高分和最低分
for(int n=0;n<5;n++){
max=scores[0][n];min=scores[0][n];
for(int i=1;i
if(scores[i][n]>max) max=scores[i][n];
else if(scores[i][n]
}
maxSingle[n]=max;minSingle[n]=min;
}
PrintWriter singleData=new PrintWriter("data/各科目資料.txt");//輸出到文檔
singleData.println("data 高數 英語 馬列 c語言 計技基礎");
singleData.print("最高分: ");for(int i=0;i<5;i++){if(i!=4)singleData.print(maxSingle[i]+" ");else singleData.println(maxSingle[i]);}
singleData.print("最低分: ");for(int i=0;i<5;i++){if(i!=4)singleData.print(minSingle[i]+" ");else singleData.println(minSingle[i]);}
singleData.print("平均分: ");for(int i=0;i<5;i++){if(i!=4)singleData.print(SingleAverage[i]+" ");else singleData.println(SingleAverage[i]);}
singleData.close();
System.out.println("操作成功:顯示為單科最高分,最低分和平均分(資料儲存在data/各科目資料.txt中)");
System.out.println("最高分:"+Arrays.toString(maxSingle)+"最低分:"+Arrays.toString(minSingle)+"平局分:"+Arrays.toString(SingleAverage));//列印檢驗
break;
case 4:
//按總分排名查詢
System.out.println("輸入總分名次:");int degree=input.nextInt();
System.out.println("第"+(degree)+"名:"+Arrays.toString(student[degree-1])+Sum[degree-1]);//排名檢索實作
break;
case 5:
//按資訊查詢結果
System.out.println("請輸入要查詢的基本資訊:名字,學号,性别");String s=input.next();
for(int i=0;i
for(int n=0;n<3;n++){
if(s.equals(student[i][n])){
System.out.println("該人資訊為:"+Arrays.toString(student[i])+" 高數:"+scores[i][0]+" 英語:"+scores[i][1]+" 馬列:"+scores[i][2]
+" c語言:"+scores[i][3]+" 計技基礎:"+scores[i][4]+"總分:"+Sum[i]+"總分第"+(i+1)+"名");
}
}
}break;
case 6:
//列印成績報表
PrintWriter form=new PrintWriter("data/成績報表.txt");form.println(" 班級成績報表 ");
form.println("學号 姓名 高數 英語 馬列 c語言 計技基礎 總分 ");
for(int i=0;i
for(int n=0;n<2;n++){form.print(student[i][n]+" ");}
for(int n=0;n<5;n++){form.print(scores[i][n]+" ");}
form.println(" "+Sum[i]);
}form.close();
System.out.println("操作成功,資料儲存在data/成績報表.txt中");
break;
default://輸入無效是報錯。
System.out.println("輸入無效!\n系統自動退出");
break;
}
System.out.println("繼續輸入任意數字,退出輸入out");
enter=input.next();
}
System.out.println("已經安全退出系統!");
}
}