📄 score.java
字号:
import java.util.*;
import java.io.*;
import javax.microedition.rms.*;
public class Score{
private String[] times;
private int[] scores;
private int scoreNum;
private Calendar calendar;
private RecordStore rs;
private String rsName;
public Score(){
calendar=Calendar.getInstance();
rsName="HighScore";
initValue();
}
private void initValue(){
scoreNum=10;
scores=new int[scoreNum];
times=new String[scoreNum];
for(int i=0;i<scoreNum;i++){
StringBuffer temp=new StringBuffer();
temp.append(calendar.get(Calendar.YEAR));
temp.append(calendar.get(Calendar.MONTH+1));
temp.append(calendar.get(Calendar.DAY_OF_MONTH));
times[i]=temp.toString();
scores[i]=0;
}
}
public void open(){
try{
rs=RecordStore.openRecordStore(rsName,true);
if(rs.getNumRecords()>0){
loadData();
}else{
creatDefault();
}
}catch(Exception e){
System.out.println("rs open reeor!!");
}
}
public void loadScore(){
try{
open();
if(rs!=null){
close();
}
}catch(Exception e){
System.out.println("rs open reeor!!");
}
}
public void close(){
if(rs!=null){
try{
rs.closeRecordStore();
}catch(Exception e){
System.out.println("rs close reeor!!");
}
}
}
public void loadData(){
try{
for(int i=0;i<scoreNum;i++){
byte[] record=rs.getRecord(i+1);
DataInputStream dis =new DataInputStream(new ByteArrayInputStream(record,0,record.length));
scores[i]=dis.readInt();
times[i]=dis.readUTF();
}
}catch(Exception e){
System.out.println("load reeor!!");
}
}
public void creatDefault(){
try{
for(int i=0;i<scoreNum;i++){
ByteArrayOutputStream baos=new ByteArrayOutputStream(12);
DataOutputStream dos=new DataOutputStream(baos);
dos.writeInt(scores[i]);
dos.writeUTF(times[i]);
dos.flush();
dos.close();
byte[] record=baos.toByteArray();
rs.addRecord(record,0,record.length);
}
}catch(Exception e){
System.out.println("creat default data error!!");
}
}
public void updateScores(int score){
try{
for(int i=0;i<scoreNum;i++){
if(score>scores[i]){
open();
for(int j=scoreNum-1;j>i;j--){
scores[j]=scores[j-1];
times[j]=times[j-1];
}
scores[i]=score;
StringBuffer temp=new StringBuffer();
temp.append(calendar.get(Calendar.YEAR));
temp.append(calendar.get(Calendar.MONTH+1));
temp.append(calendar.get(Calendar.DAY_OF_MONTH));
times[i]=temp.toString();
updateDate();
if(rs!=null){
close();
}
break;
}
}
}catch(Exception e){
System.out.println("error!!");
}
}
private void updateDate(){
try{
for(int i=0;i<scoreNum;i++){
ByteArrayOutputStream baos=new ByteArrayOutputStream(12);
DataOutputStream dos=new DataOutputStream(baos);
dos.writeInt(scores[i]);
dos.writeUTF(times[i]);
dos.flush();
dos.close();
byte[] record=baos.toByteArray();
rs.setRecord(i+1,record,0,record.length);
}
}catch(Exception e){
System.out.println("error!!");
}
}
public int[] getScores(){
return scores;
}
public String[] getTimes(){
return times;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -