2014-02-26 139 views
-4

當我嘗試返回結果錯誤:不兼容的Java類型

error: incompatible types 
char[][]result = ' '; 
       ^
required: char[][] 
found: char 

在這段代碼中我得到這個錯誤信息。 我需要改變結果爲char [] [] 我的代碼的另一部分,但不知道如何做到這一點。 我只是不能得到它。

public class Encryptor { 

private int N; 

public Encryptor(int N) { 
this.N = N; 
} 
////////////////////added///////////////// 

public char[][] encrypt(char[][] P) { 
char ch; 
char[][]result = ' '; 

for (int i = 0; i < P.length; i++) { 
    for (int j = 0; j < P[0].length; j++) { 

ch = P[i][j]; 
    if ((ch >= '0') && (ch <= '9')) { 
    ch = rotate(ch, '0', '9'); 
    } else if ((ch >= 'A') && (ch <= 'Z')) { 
    ch = rotate(ch, 'A', 'Z'); 
    } else if ((ch >= 'a') && (ch <= 'z')) { 
    ch = rotate(ch, 'a', 'z'); 
    } else { 
    // no need to encrypt 
    } 
result += ch; 
} 
} 
return result; 
} 
/////////////////////////////////////////// 


private char rotate(char c, char min, char max) { 
int counter = N; 

while(counter > 0) { 
    c++; 
    if (c > max) c = min; 
    counter--; 
} 
return c; 
} 

public String encrypt(double D) { 
return encrypt("" + D); 
} 

public int getN() { 
return N; 
} 
} 

非常感謝您。

+0

你期望什麼'char [] [] result ='';'要做什麼? –

+0

你來自javascript嗎? –

回答

3
char[][] result = ' '; 

' ' char類型的,resultchar[][]型(char陣列的陣列)的。

正如編譯器所說的那樣,它們是不兼容的類型

有兩種解決方法:

  • 古怪的解決方案:char[][] result = {{' '}};
  • 不奇怪的解決方案:char result = ' ';
0

' '值是char,而不是一個char[][]
所以你不能這樣做。

0
char[][]result = ' '; 

' '是單個字符所以其incomitable到2D陣列。 2D陣列與{{},{},..}

char[][] result = {{' '}}; 

這裏初始化結果[0] [0]將是' '