• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

J2ME游戏中读入文本并存储在String数组

servlet/jsp 搞代码 7年前 (2018-06-18) 66次浏览 已收录 0个评论

一个在J2SE中十分钟即告完成的代码,在J2ME中花费了一个下午.是什么样的代码呢?
[要求]在游戏中读入文本形式存储的hints

[分析]
J2ME没有BufferReader,用InputStreamReader吧
既然没有BufferReader,也就不要奢望有readLine()了,自己慢慢循环吧

http://www.gaodaima.com/?p=44736J2ME游戏中读入文本并存储在String数组

[源代码]

J2ME游戏中读入文本并存储在String数组 以下内容为程序代码
/* read hint————————————– */
/**
* This is the hints
* */
String[][] hintStr;

/**
* this specify the hint’s show width
*/
final int hint_width = 8;

int hint_num;

public boolean readHint(int stage) {

if (stage > 0 && stage < 4) {
if (stage == 2) {
hint_num = 4;
} else {
hint_num = 3;
}
} else {
System.out.println("No this stage");
return false;
}

try {

String filename = "hint" + Integer.toString(stage);
InputStreamReader in = new InputStreamReader(getClass()
.getResourceAsStream(filename));
int num = -1;
do {
++num;
} while (in.read() != -1);
in.reset();
char[] hintsdat = new char[num];
System.out.println(num);
in.read(hintsdat);
in.close();

String hint_temp = new String(hintsdat);

int j = 0;
int i = 0;
String hint_num_temp[] = new String[hint_num];
for (int x = 0; x < hint_num; x++)
hint_num_temp[x] = "";
while (true) {

if (i < hintsdat.length) {
if (hintsdat[i] != ‘/n’) {
hint_num_temp[j] += hintsdat[i];
i++;
continue;
} else if (j < hint_num) {
System.out.println(i);
i++;
j++;
}
continue;
}

break;
}

// System.out.println("Creating….");

j = 0;
for (i = 0; i < hint_num; i++) {
String str = new String(hint_num_temp[i]);
int str_num = str.length() / hint_width + 1;
hintStr = new String[num][str_num];
for (int z = 0; z < str_num; z++) {
if (z < str_num – 1) {
hintStr[j][z] = "";
hintStr[j][z] += str.substring(z * hint_width, (z + 1)
* hint_width);
} else {
hintStr[j][z] = "";
hintStr[j][z] += str.substring((z – 1) * hint_width);
}
System.out.println("hintStr[" + j + "][" + z + "]:"
+ hintStr[j][z]);
}
j++;
}

} catch (IOException e) {
System.out.println("Error");
}
return true;
}

[总结]
程序要求很简单,一定有更好的办法来实现,结果参阅了大量资料,一无所获.

欢迎大家阅读《J2ME游戏中读入文本并存储在String数组》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:J2ME游戏中读入文本并存储在String数组

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址