【问题标题】:trying to convert a code block from C# to Java (Android)尝试将代码块从 C# 转换为 Java (Android)
【发布时间】: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

标签: c# java android


【解决方案1】:

你没有说它在哪里,但我敢打赌它就在这里......

line[colCurrentLevel*screenCols + currentCol]

应该使用String's charAt(),因为在 Java 中行不被视为 char 数组...

line.charAt(colCurrentLevel*screenCols + currentCol)

【讨论】:

    【解决方案2】:

    我认为问题是在java中String类不支持[]操作符来访问特定的字符,所以你需要调用line.charAt(colCurrentLevel*screenCols + currentCol)而不是line[colCurrentLevel*screenCols + currentCol]

    【讨论】:

      【解决方案3】:

      您无法使用[] 访问角色 请改用line.charAt(colCurrentLevel*screenCols + currentCol);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-14
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 2014-05-27
        • 2015-09-27
        • 2020-11-26
        相关资源
        最近更新 更多