- import java.util.Map;
- import java.util.HashMap;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- public class ScoreStat
- {
- public static void main(String[] args) throws IOException
- {
- for(int i = 1; i <= 5; i++){
- System.out.println("Please input student "+i+" related infomation:");
- Map<String,String> stuInfo = getInput();
- double avg = avg(Double.parseDouble(stuInfo.get("Chinese")),Double.parseDouble(stuInfo.get("Math")),Double.parseDouble(stuInfo.get("English")));
- stuInfo.put("Avg",avg+"");
- if(save(stuInfo)){
- System.out.println("OK");
- }
- else{
- System.out.println("Error");
- }
- }
- }
- private static Map<String,String> getInput() throws IOException{
- Map<String,String> stu = new HashMap<String,String>();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String buf = null;
- System.out.println("********************************************");
- System.out.print("Student No: ");
- buf = br.readLine();
- stu.put("stuNo",buf);
- System.out.print("Student name: ");
- buf = br.readLine();
- stu.put("Name",buf);
-
- System.out.print("Score of Chinese: ");
- buf = br.readLine();
- stu.put("Chinese",buf);
-
- System.out.print("Score of Math: ");
- buf = br.readLine();
- stu.put("Math",buf);
- System.out.print("Score of English: ");
- buf = br.readLine();
- stu.put("English",buf);
- return stu;
- }
- private static double avg(double score1,double score2,double score3){
- return (score1 + score2 + score3) / 3;
- }
- private static boolean save(Map<String,String> stuInfo) throws IOException{
- BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\scores.txt",true));
- bw.write("StuNo: "+stuInfo.get("stuNo"));
- bw.newLine();
- bw.write("Name: "+stuInfo.get("Name"));
- bw.newLine();
- bw.write("Chinese: "+stuInfo.get("Chinese"));
- bw.newLine();
- bw.write("Math: "+stuInfo.get("Math"));
- bw.newLine();
- bw.write("English: "+stuInfo.get("English"));
- bw.newLine();
- bw.write("Avg: "+stuInfo.get("Avg"));
- bw.newLine();
- bw.write("***********************");
- bw.newLine();
- bw.close();
- return true;
- }
- }
复制代码
貌似木有做,是不是发的地方不对,浏览的人太少。 |