⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 score.java

📁 j2me开发 简单示例 包括 游戏主菜单、rms功能模块
💻 JAVA
字号:
package com.gowin.firstgame;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class Score extends BaseRms {

	private String[] names = new String[3];
	private int[] values = new int[3];
	
	Score(String rmsName) {
		super(rmsName);
		initValues();
	}

	private void initValues() {
		names[0] = "lje";
		names[1] = "fg";
		names[2] = "yyc";
		values[0] = 1;
		values[1] = 2;
		values[2] = 3;	
	}

	public String[] getNames() {
		return names;
	}


	public int[] getValues() {
		return values;
	}

	public void  loadScores(){
		this.open();
		if( getRecordStore() != null){
			close();
		}
	}
	
	public void reset(){
		this.open();
		initValues();
		try {
			updateData();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		if( getRecordStore() != null){
			close();
		}
	}
	
	public void updateScores(int score, String name){
		for(int i = 0 ; i < names.length ; i ++ ){
			if(score > values[i]){
				this.open();
				for( int j = names.length-1 ; j > i ; j--){
					values[j] = values[j-1];
					names[j] = names[j-1];
				}
				this.names[i] = name;
				this.values[i] = score;
				try {
					updateData();
				} catch (Exception e) {
					e.printStackTrace();
				}
				if( getRecordStore()!= null){
					close();
				}
				break;
			}			
		}
	}

	public boolean isHightScore(int score){
		boolean isHightScore = false;
		for( int i = 0 ; i < names.length ; i ++ ){
			if( score > values[i] ){
				isHightScore = true;
				break;
			}
		}
		return isHightScore;
	}
	
	
	void createDefaultData() throws Exception {
		for( int i = 0 ; i < names.length ; i ++){
			ByteArrayOutputStream bStream = new ByteArrayOutputStream(12);
			DataOutputStream oStream = new DataOutputStream(bStream);
			oStream.writeInt( values[i] );
			oStream.writeUTF( names[i] );
			oStream.flush();
			oStream.close();
			byte[] record = bStream.toByteArray();
			getRecordStore().addRecord(record, 0, record.length);
		}
	}

	
	
	void loadData() throws Exception {
		for( int i = 0; i < names.length ; i++){
			byte[] record = getRecordStore().getRecord(i+1);
			DataInputStream iStream = new DataInputStream( 
					new ByteArrayInputStream(record, 0 , record.length));
			values[i] = iStream.readInt();
			names[i] = iStream.readUTF();			
		}
	}

	void updateData() throws Exception {
		for( int i = 0 ; i < names.length ; i ++){
			ByteArrayOutputStream bStream = new ByteArrayOutputStream(12);
			DataOutputStream oStream = new DataOutputStream(bStream);
			oStream.writeInt( values[i] );
			oStream.writeUTF( names[i] );
			oStream.close();
			byte[] record = bStream.toByteArray();
			getRecordStore().setRecord(i+1 , record, 0, record.length);
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -