?? highscorecomparator.java
字號(hào):
/*
* HighScoreComparator.java
*
* Created on 2006年6月8日, 下午4:03
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
/**
*
* @author Administrator
*/
class HighScoreRms implements RecordComparator {
public int compare(byte[] rec1, byte[] rec2){
int p1=0;
int p2=0;
try{
HighScoreComparator.deserialize(rec1);
p1=HighScoreComparator.point;
HighScoreComparator.deserialize(rec2);
p2=HighScoreComparator.point;
}catch(Exception e){}
if (p1==p2){
return RecordComparator.EQUIVALENT;
} else if(p1>p2){
return RecordComparator.PRECEDES;
} else{
return RecordComparator.FOLLOWS;
}
}
}
public class HighScoreComparator{
/** Creates a new instance of HighScoreComparator */
public HighScoreComparator() {
}
public static String playName="";
public static int point=0;
public static String[] plays=null;
public static int[] points=null;
public static byte[] data=null;
public static byte[] serialize(String pName,int pt) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(pName);
dos.writeInt(pt);
baos.close();
dos.close();
return baos.toByteArray();
}
public static void deserialize(byte[] data1) throws IOException{
ByteArrayInputStream bais = new ByteArrayInputStream(data1);
DataInputStream dis = new DataInputStream(bais);
playName=dis.readUTF();
point=dis.readInt();
bais.close();
dis.close();
}
public static int getMaxPoint(){
int r=0;
int id=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
}catch(Exception e){
}
if (rs1!=null){
RecordEnumeration re=null;
try{
re=rs1.enumerateRecords(null,new HighScoreRms(),false);
if(re.hasNextElement()){
id=re.nextRecordId();
data=rs1.getRecord(id);
deserialize(data);
}
}catch(Exception e){
}
if(re!=null){
re.destroy();
}
r=point;
re=null;
try{
rs1.closeRecordStore();
}catch(Exception e){
}
rs1=null;
}
return r;
}
//返回值-1代表沒有此記錄或記數(shù)個(gè)數(shù)少于5
public static int getMinRecId(){
int r=-1;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
}catch(Exception e){}
RecordEnumeration re=null;
if (rs1!=null){
try{
r=rs1.getNumRecords();//得到刻錄的總個(gè)數(shù)
}catch(Exception e){}
if (r<5){
r=-1;
}else{
try{
re=rs1.enumerateRecords(null,new HighScoreRms(),false);
while(re.hasNextElement()){
r=re.nextRecordId();
}
}catch(Exception e){}
if(re!=null){
re.destroy();
}
re=null;
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
return r;
}
/**
*保存記錄
*@parameter name:玩家名子
*pot:玩家的得分
*/
public static void saveRec(String name,int pot){
try{
data=HighScoreComparator.serialize(name,pot);
}catch(Exception e){}
int min=getMinRecId();
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
} catch(Exception e){}
if (rs1!=null){
if (min==-1){
try{
rs1.addRecord(data,0,data.length);
}catch(Exception e){}
}else{
try{
rs1.setRecord(min,data,0,data.length);
}catch(Exception e){}
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
}
//返回值代表已經(jīng)存儲(chǔ)的記錄個(gè)數(shù)
public static int setHighScoreList(){
int r=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
r=rs1.getNumRecords();
}catch(Exception e){}
if (r>0){
plays=new String[r];
points=new int[r];
try{
RecordEnumeration re=rs1.enumerateRecords(null,new HighScoreRms(),false);
int i=0;
while(re.hasNextElement()){
data=re.nextRecord();
deserialize(data);
plays[i]=playName;
points[i]=point;
i++;
}
if(re!=null){
re.destroy();
}
}catch(Exception e){}
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
return r;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -