【发布时间】:2013-04-18 03:33:29
【问题描述】:
我正在尝试将我用 C# 编写的方法转换为适用于 Android 的 Java。
以下编码是我尝试从 C# 转换为 Java 的代码:
public Map()
{
string line;
int maxRowLevels, maxColLevels;
StreamReader file;
file = File.OpenText("map.txt");
line = file.ReadLine();
line = file.ReadLine();
maxRowLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
line = file.ReadLine();
maxColLevels = System.Convert.ToInt32(line) - 1;
line = file.ReadLine();
map = new char[maxRowLevels+1,maxColLevels+1,screenRows,screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.ReadLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
map[rowCurrentLevel, colCurrentLevel,currentRow, currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
file.Close();
}
我的转换尝试如下代码:
public Map(Context context)
{
String line;
int maxRowLevels, maxColLevels;
int maxRowLevels, maxColLevels;
InputStream inputFile;
BufferedReader file;
inputFile = context.getAssets().open("map.txt");
file = new BufferedReader(new InputStreamReader(inputFile, "UTF-8"));
line = file.readLine();
line = file.readLine();
maxRowLevels = Integer.parseInt(line)-1;
line = file.readLine();
line = file.readLine();
maxColLevels = Integer.parseInt(line)-1;
line = file.readLine();
mapa = new char[maxRowLevels+1][maxColLevels+1][screenRows][screenCols];
for (int rowCurrentLevel = 0; rowCurrentLevel<=maxRowLevels; rowCurrentLevel++)
{
for (int currentRow = 0; currentRow<screenRows; currentRow++)
{
line = file.readLine();
for (int colCurrentLevel = 0; colCurrentLevel<=maxColLevels; colCurrentLevel++)
{
for (int currentCol = 0; currentCol<screenCols; currentCol++)
{
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol];
}
}
}
}
inputFile.close();
file.close();
}
似乎一切都是正确的,除了这一行删除了这个错误:“表达式的类型必须是数组类型的字符串,但它解析为字符串”
mapa[rowCurrentLevel][colCurrentLevel][currentRow][currentCol] = line[colCurrentLevel*screenCols + currentCol];
谁能告诉我如何解决它? 我认为这很愚蠢,请原谅我,但我是 Java 初学者。
提前致谢。
【问题讨论】:
-
c#标签加了,不知道是不是应该是c++,但肯定不是c -
第二行有一个错字“我正在尝试将我编写的 i C 方法转换为 Android 的 java。” , 应该是 C# 而不是 i c