为什么我一直需要:char [] [] found:java中的char

时间:2014-02-26 08:06:24

标签: java

只是想知道为什么我

error: incompatible types? 
safePicture = C.encrypt(picture);
                           ^
required: char[][]
found:    char

我无法弄明白。感谢

public class h {

public static void main( String[] args ) {
    Cryptor C            = new Cryptor( 3 );
    char[][] picture     = buildHouse();
    char[][] safePicture, origPicture;


    printPicture( picture );
    safePicture = C.encrypt(picture);
    printPicture( safePicture );
    origPicture = C.decrypt( picture );
    printPicture( origPicture );
}

public static void printPicture( char[][] picture ) {
for ( int i = 0; i < picture.length; i++ ) {
    for ( int j = 0; j < picture[0].length; j++ ) {
    System.out.print( picture[ i ][ j ] );
    }
    System.out.println();
}
}

public static char[][] buildHouse() {
char[][] tmp = { {'+',
'0',
'0',
'+'}
};
return tmp;
}
}

好的我认为错误来自Cryptor类。 我不确定是否返回E.encrypt(P);导致错误。 任何帮助将不胜感激。

public class Cryptor {
private Encryptor E;
private Decryptor D;

public Cryptor() {
E = new Encryptor( 13 );
D = new Decryptor( 13 );
}

public Cryptor( int N ) {
E = new Encryptor( N );
D = new Decryptor( N );
}

public Encryptor getEncryptor() {
return E;
}

public Decryptor getDecryptor() {
return D;
}

public int getN() {
return E.getN();
}
//////////////////////////////added/////////////
public char encrypt(char[][]P ) {
return E.encrypt( P );
}

////////////////////////////////////////////
public String encrypt( String S ) {
return E.encrypt( S );
}

public String encrypt( double D ) {
return E.encrypt( D );
}

public String decrypt( String S ) {
return D.decrypt( S );
}

public String decrypt( double D ) {
return this.D.decrypt( D );
}
}

这也是Encryptor类。 不确定这是否导致了char错误。

public class Encryptor {

private int N;

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

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

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;
}
///////////////////////////////////////////
public String encrypt( String S ) {
char ch;
String result = "";

// Loop through the String S;
for ( int index = 0; index < S.length(); index++ ) {
    ch = S.charAt( index );
    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 个答案:

没有答案