一個在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]
[總結]
程序要求很簡單,一定有更好的辦法來實現,結果參閱了大量資料,一無所獲.