📄 score.java
字号:
/* * Score.java * * Created on 2007年12月19日, 下午9:46 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package SBGameCore;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Vector;/** * * @author wusir */public class Score { /** Creates a new instance of Score */ File Score = null; String name_score = null; Writer writer = null; InputStream is = null; public Score() { Score = new File("score"); } public void input(String record){ try { writer = new FileWriter(Score,false); writer.write(record); } catch (IOException e){ e.printStackTrace(); } finally { if (writer != null){ try { writer.close(); } catch (IOException e1) { } } } } public String output(){ try { try { is = new FileInputStream(Score) ; } catch (FileNotFoundException ex) { ex.printStackTrace(); } long contentLength = Score.length(); byte[] ba = new byte[(int)contentLength]; try { is.read(ba); } catch (IOException ex) { ex.printStackTrace(); } name_score = new String(ba); }finally { if(is!=null) { try{is.close(); } catch(Exception e){} } } return name_score; } public void addRecord(String name,int score){ Vector scores = new Vector(); Vector names = new Vector(); String newRecords = ""; String[] oldRecords = output().split("#"); for(int i = 0;i<oldRecords.length;i++){ names.add(oldRecords[i].split("&")[0]); scores.addElement(oldRecords[i].split("&")[1]); } if(score<=Integer.parseInt((String)scores.elementAt(scores.size()-1))); else if (score>Integer.parseInt((String)scores.elementAt(0))){ scores.add(0,score); names.add(0,name); }else{ for(int i = 0;i<scores.size()-1;i++){ if(score<=Integer.parseInt((String)scores.elementAt(i))&&score>=Integer.parseInt((String)scores.elementAt(i+1))){ scores.add(i+1,score); names.add(i+1,name); break; } } } for(int i = 0;i < 5;i++){ /*if(i<4) newRecords += names.elementAt(1)+"&"+scores.elementAt(i)+"#"; else*/ newRecords += names.elementAt(i)+"&"+scores.elementAt(i)+"#"; } input(newRecords); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -