麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

用RMS存儲游戲積分

2019-11-18 16:05:43
字體:
來源:轉載
供稿:網友
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * A class used for storing and showing game scores.
 */
public class RMSGameScores extends MIDlet implements RecordFilter,
    RecordComparator {
  /*
   * The RecordStore used for storing the game scores.
   */
  PRivate RecordStore recordStore = null;

  /*
   * The player name to use when filtering.
   */
  public static String playerNameFilter = null;

  /**
   * The constUCtor opens the underlying record store, creating it if
   * necessary.
   */
  public RMSGameScores() {
    // Create a new record store for this example
    try {
      recordStore = RecordStore.openRecordStore("scores", true);
    } catch (RecordStoreException rse) {
      System.out.println("Record Store Exception in the ctor." + rse);
      rse.printStackTrace();
    }
  }

  /**
   * startApp()
   */
  public void startApp() throws MIDletStateChangeException {
    RMSGameScores rmsgs = new RMSGameScores();
    rmsgs.addScore(100, "Alice");
    rmsgs.addScore(120, "Bill");
    rmsgs.addScore(80, "Candice");
    rmsgs.addScore(40, "Dean");
    rmsgs.addScore(200, "Ethel");
    rmsgs.addScore(110, "Farnsworth");
    rmsgs.addScore(220, "Alice");
    RMSGameScores.playerNameFilter = "Alice";
    System.out
        .println("Print all scores followed by Scores for Farnsworth");
    rmsgs.printScores();
  }

  /*
   * Part of the RecordFilter interface.
   */
  public boolean matches(byte[] candidate) throws IllegalArgumentException {
    // If no filter set, nothing can match it.
    if (this.playerNameFilter == null) {
      return false;
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
    DataInputStream inputStream = new DataInputStream(bais);
    String name = null;

    try {
      int score = inputStream.readInt();
      name = inputStream.readUTF();
    } catch (EOFException eofe) {   System.out.println(eofe);
      eofe.printStackTrace();
    } catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    return (this.playerNameFilter.equals(name));
  }

  /*
   * Part of the RecordComparator interface.
   */
  public int compare(byte[] rec1, byte[] rec2) {

    // Construct DataInputStreams for extracting the scores from
    // the records.
    ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
    DataInputStream inputStream1 = new DataInputStream(bais1);
    ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
    DataInputStream inputStream2 = new DataInputStream(bais2);
    int score1 = 0;
    int score2 = 0;
    try {
      // Extract the scores.
      score1 = inputStream1.readInt();
      score2 = inputStream2.readInt();
    } catch (EOFException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    } catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }

    // Sort by score
    if (score1 > score2) {
      return RecordComparator.FOLLOWS;
    } else if (score1 < score2) {
      return RecordComparator.PRECEDES;
    } else {
      return RecordComparator.EQUIVALENT;
    }
  }

  /**
   * Add a new score to the storage.
   * 
   * @param score
   *            the score to store.
   * @param playerName
   *            the name of the play achieving this score.
   */
  public void addScore(int score, String playerName) {
    // Each score is stored in a separate record, formatted with
    // the score, followed by the player name.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baos);
    try {
      // Push the score into a byte array.
      outputStream.writeInt(score);
      // Then push the player name.
      outputStream.writeUTF(playerName);
    } catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }

    // Extract the byte array
    byte[] b = baos.toByteArray();
    try {
      // Add it to the record store
      recordStore.addRecord(b, 0, b.length);
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
  }

  /**
   * A helper method for the printScores methods.
   */
  private void printScoresHelper(RecordEnumeration re) {

    try {
      while (re.hasNextElement()) {
        int id = re.nextRecordId();
        ByteArrayInputStream bais = new ByteArrayInputStream(
            recordStore.getRecord(id));
        DataInputStream inputStream = new DataInputStream(bais);
        try {
          int score = inputStream.readInt();
          String playerName = inputStream.readUTF();
          System.out.println(playerName + " = " + score);
        } catch (EOFException eofe) {
          System.out.println(eofe);
          eofe.printStackTrace();
        }
      }
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    } catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }
  }

  /**
   * This method prints all of the scores sorted by game score.
   */
  public void printScores() {
    try {
      // Enumerate the records using the comparator implemented
      // above to sort by game score.

      // No RecordFilter here. All records in the RecordStore
      RecordEnumeration re = recordStore.enumerateRecords(null, this,
          true);

      // Print all scores
      System.out.println("Print all scores...");
      printScoresHelper(re);

      // Enumerate records respecting a RecordFilter
      re = recordStore.enumerateRecords(this, this, true);

      //Print scores for Farnsworth
      System.out.println("Print scores for : " + this.playerNameFilter);
      printScoresHelper(re);
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }}

  /**
   * pauseApp()
   */
  public void pauseApp() {
    System.out.println("pauseApp()");
  }

  /**
   * destroyApp()
   * 
   * This closes our open RecordStore when we are destroyed.
   * 
   * @param cond
   *            true if this is an unconditional destroy false if it is not
   *            (ignored here and treated as unconditional)
   */
  public void destroyApp(boolean cond) {
    System.out.println("destroyApp( )");
    try {
      if (recordStore != null)
        recordStore.closeRecordStore();
    } catch (Exception ignore) {
      // ignore this
    }
  }

}

(出處:http://www.companysz.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本不卡一区二区三区在线观看 | 99riav国产在线观看 | 欧美五月婷婷 | 爱逼av| 欧美一级特黄a | 视频一区二区视频 | 国产精品成人免费一区久久羞羞 | 亚洲精品a在线观看 | 成人三级电影网址 | 欧美成人免费看 | 国产亚洲精品综合一区91 | 美国一级黄色毛片 | 日日爱影院 | 黄色大片网站在线观看 | 男女做性免费网站 | 4p一女两男做爰在线观看 | 成人综合免费视频 | 国产69精品久久久久久 | 欧美精品电影一区二区 | 国产精品久久久久久久久久三级 | 巨乳激情 | 国产资源在线视频 | 草久影视| 精品久久久久久久久久久aⅴ | 蜜桃传媒视频麻豆第一区免费观看 | 国产一级爱c视频 | 亚洲成人福利在线 | 国产午夜精品理论片a级探花 | 亚洲免费看片网站 | 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 成人18网站 | 午夜在线观看视频网站 | 中文字幕亚洲一区二区三区 | 欧美日本综合 | 亚洲av一级毛片特黄大片 | 综合在线一区 | 久久国产秒 | 黄在线观看在线播放720p | 欧美性受ⅹ╳╳╳黑人a性爽 | 国产精品视频二区不卡 | 欧美精品一区二区中文字幕 |