2011-03-13 97 views
2

我試圖將我用C#編寫的方法轉換爲適用於Android的Java。試圖將C#代碼塊轉換爲Java(Android)

下面的編碼是什麼,我想從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的begginer。

在此先感謝。

+0

'c#'標籤添加,我不知道它應該是'C++',但是它肯定不是'c' – pmg 2011-03-13 19:44:33

+0

在第二行有一個錯字「我試圖將我已經編寫了C的方法轉換爲Java的Android。」 ,應該在C#而不是我c – 2011-03-13 19:47:01

+0

請參閱:http://stackoverflow.com/questions/78811/is-there-an-effective-tool-to-convert-c-sharp-code-to-java-code – 2012-11-06 17:59:05

回答

3

你不說它在哪裏,但我打賭它是在這裏...

line[colCurrentLevel*screenCols + currentCol] 

哪些應該使用String's charAt()因爲行不作爲Java的字符數組處理...

line.charAt(colCurrentLevel*screenCols + currentCol) 
1

不能接取字符與[] 使用line.charAt(colCurrentLevel*screenCols + currentCol);代替

2

stirng我認爲問題是,在JA va的String類不支持[]運算符來訪問一個特定的字符,所以你需要調用line.charAt(colCurrentLevel*screenCols + currentCol)而不是line[colCurrentLevel*screenCols + currentCol]