错误:不兼容的Java类型 [英] error: incompatible types java
本文介绍了错误:不兼容的Java类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误信息,当我尝试返回结果。
错误:不兼容的类型
的char [] []结果='';
^
要求:的char [] []
发现:CHAR
在此code。
我需要的结果改变成char [] []为的另一部分
我的code,但不知道该怎么做。
我只是不能似乎得到它。
公共类加密器{私人诠释N;公共加密器(INT N){
this.N = N;
}
////////////////////添加/////////////////公众的char [] []加密(的char [] [] P){
焦炭CH;
的char [] []结果='';的for(int i = 0; I< P.length;我++){
为(中间体J = 0; J&下,P [0]。长度; J ++){CH = P [I] [J]。
如果((CH&GT ='0')及及(CH&下; ='9')){
CH =旋转(CH,'0','9');
}否则如果((CH> ='A')及及(CH< ='Z')){
CH =旋转(CH,'A','Z');
}否则如果((CH> ='一')及及(CH&下; ='Z')){
CH =移位(CH,'一个','Z');
}其他{
//不需要加密
}
结果+ = CH;
}
}
返回结果;
}
///////////////////////////////////////////
私人旋转字符(字符C,焦分,字符最大){
INT计数器= N;而(计数器大于0){
C ++;
如果(c取代;最大)C =分钟;
计数器 - ;
}
返回℃;
}公共字符串加密(双D){
返回加密(+ D);
}公众诠释getN(){
返回N;
}
}
感谢您提前。
解决方案
的char [] []结果='';
是键入的字符,
结果
的类型为<$ C的$ C>的char [] [] (数组字符
阵列)。
由于编译器说,准确地说,他们的不兼容的类型的
您有两种解决方法:
- 古怪的解决方案:
的char [] []结果= {{''}};
- 不奇怪解决方案:
字符结果='';
I get this error message when i try to return result
error: incompatible types
char[][]result = ' ';
^
required: char[][]
found: char
In this code. I need to change the result into char [][] for another portion of my code but not sure how to do that. I just cant seem to get it.
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;
}
}
thank you in advance.
解决方案
char[][] result = ' ';
' '
is of type char, result
is of type char[][]
(array of char
array).
As the compiler says, exactly, they are incompatible types.
You have two solutions:
- Weird solution:
char[][] result = {{' '}};
- Not weird solution:
char result = ' ';
这篇关于错误:不兼容的Java类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文