日期:2014-05-20 浏览次数:20972 次
package org.example.guessdigit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.example.io.FileIO;
public class Settings {
public final static String file = ".GuessDigit";
public final static int[][] highscores = new int[3][5];
public final static String[][] players = new String[][]{
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
};
public static void load(FileIO files){
BufferedReader in = null;
try{
in = new BufferedReader(new InputStreamReader(files.readFile(file)));
// 读取分数
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
highscores[i][j] = Integer.parseInt(in.readLine());
}
}
// 读取玩家
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
players[i][j] = in.readLine();
}
}
}catch(IOException ex){
ex.printStackTrace();
}catch(NumberFormatException e){ //load方法里会有NumberFormatException
e.printStackTrace();
}finally{
try{
if(in != null)
in.close();
}catch(IOException e){
}
}
}
public static void save(FileIO files){
BufferedWriter out = null;
try{
out = new BufferedWriter(new OutputStreamWriter(
files.writeFile(file)));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
out.write(highscores[i][j]);
out.write("\n");
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
out.write(players[i][j]);
out.write("\n");
}
}
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
if(out != null)
out.close();
}catch(IOException e){
}
}
}
public static void addScore(int diff, String player, int score){
for(int i = 0; i < 5; i++){
if(score > highscores[diff - 3][i]){
for(int j = 4; j > i; j--){
highscores[diff - 3][j] = highscores[diff - 3][j - 1];
players[diff - 3][j] = players[diff - 3][j - 1];
}
highscores[diff - 3][i] = score;
players[diff - 3][i] = player;
break;
}
}
}
}
Settings.addScore(diff, player, score);
Settings.save(file);
Setting.load(file); // load方法里会有NumberFormatException