• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 11次 | 进入软件测试论坛讨论

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

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

[源代码]
[code]
/* 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;
}
[/code]
[总结]
程序要求很简单,一定有更好的办法来实现,结果参阅了大量资料,一无所获.

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网