/*å®å
¨æç
§ä½ çè¦æ±åçï¼å¯ä»¥çç»é纳ãæä»ä¹é®é¢å¯ä»¥ç»§ç»é®æã*/
package Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* æ件ä¿åæ ¼å¼ä¸º
* å¦å·,å§å,ç级,javaæ绩
* å¦å·,å§å,ç级,javaæ绩
* å¦å·,å§å,ç级,javaæ绩
* å¦å·,å§å,ç级,javaæ绩
* å¦å·,å§å,ç级,javaæ绩
* æ»å,å¹³åå
* @author Administrator
*
*/
public class ZhiDao {
public static void main(String[] args) {
File file = new File("javascore.text");
List<String[]> infos = null;
if (file.exists()) {
System.out.println("å·²æä¿¡æ¯ï¼");
infos = getInfos(file);
System.out.println(printInfos(infos));
System.exit(0);
} else {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("æ æ³å建æ件" + e.getMessage());
System.exit(1);
}
}
infos = new ArrayList<String[]>();
Scanner scan = new Scanner(System.in);
String info = null;
String[] strs = null;
int count = 0;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
float totalScore = 0;
while (count < 5) {
System.out.println("请è¾å
¥ç¬¬ " + (count + 1) + "åå¦ççåºæ¬ä¿¡æ¯[åå«ä¸ºå¦å·ãå§åãç级ãjavaæ绩ï¼ä»¥è±æéå·åé]ï¼");
info = scan.next().trim();
//å¤ææ绩åæ³æ§
strs = info.split(",");
if (strs.length != 4) {
System.out.println("è¾å
¥ä¿¡æ¯ä¸æ£ç¡®,请éæ°è¾å
¥!");
continue;
}
try {
if (Float.valueOf(strs[3]) > 100) {
System.out.println("æ绩ä¸è½è¶
è¿100å!");
continue;
}
} catch (NumberFormatException e) {
// TODO: handle exception
System.out.println("æ绩è¾å
¥æ误!" + e.getMessage());
continue;
}
totalScore += Float.valueOf(strs[3]);
infos.add(strs);
count++;
}
infos.add(new String[]{nf.format(totalScore), nf.format(totalScore / 5)});
System.out.println(printInfos(infos));
saveInfos(infos);
System.out.println("ä¿åå®æï¼");
}
public static String printInfos(List<String[]> infos) {
String str = "";
for (String[] ss:infos) {
if (ss.length == 2) {
str += "æ»åï¼" + ss[0] + "\nå¹³ååï¼" + ss[1] + "\n";
continue;
}
str += "****************************\n" + "å¦å·ï¼" + ss[0] + "\n"
+ "å§åï¼" + ss[1] + "\n"
+ "ç级ï¼" + ss[2] + "\n"
+ "javaæ绩ï¼" + ss[3] + "\n\n\n";
}
return str;
}
public static void saveInfos(List<String[]> infos){
FileWriter writer = null;
BufferedWriter buffWriter = null;
try {
writer = new FileWriter(new File("javascore.text"));
buffWriter = new BufferedWriter(writer);
String str = null;
for (String[] ss:infos) {
str = ss[0] + "," + ss[1];
if (ss.length > 2) {
str += "," + ss[2] + "," + ss[3];
}
buffWriter.write(str);
buffWriter.newLine();
}
buffWriter.flush();
} catch (Exception e) {
// TODO: handle exception
System.out.println("ä¿åä¿¡æ¯å°æ件åºéï¼" + e.getMessage());
System.exit(1);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (buffWriter != null) {
try {
buffWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static List<String[]> getInfos(File f) {
FileReader reader = null;
BufferedReader buffReader = null;
List<String[]> infos = new ArrayList<String[]>();
try {
reader = new FileReader(f);
buffReader = new BufferedReader(reader);
String str = null;
while ((str = buffReader.readLine()) != null) {
infos.add(str.split(","));
}
} catch (Exception e) {
System.out.println("ä»æ件ä¸è¯»åä¿¡æ¯åºé..." + e.getMessage());
System.exit(1);
//e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (buffReader != null) {
try {
buffReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return infos;
}
}
温馨提示:答案为网友推荐,仅供参考