2013-11-26 138 views
1

我不知道什么是错的,但是我不能让这个方法返回我想要的字符。在“返回”一词下,我只是不断收到红线形式的错误。从方法中返回一个字符

这是我的方法。

public static char getOperator(String fileContent){ 


    int checkAdd = fileContent.indexOf('+'); 
    int checkMinus = fileContent.indexOf('-'); 
    int checkMulti = fileContent.indexOf('*'); 
    int checkDivi = fileContent.indexOf('/'); 

    if (checkAdd != -1){ 
     return char operator = fileContent.charAt(fileContent.indexOf('+')); 
    } 
    else if (checkMinus != -1) { 
     return char operator = fileContent.charAt(fileContent.indexOf('-')); 
    } 
    else if (checkMulti != -1) { 
     return char operator = fileContent.charAt(fileContent.indexOf('*')); 
    } 
    else if (checkDivi != -1){ 
     return char operator = fileContent.charAt(fileContent.indexOf('/')); 
    } 
    return 0; 


    } 

好的,对不起,我一直在蚀它在日食,并没有抓住这个问题。但是,现在ifs中的返回语句仍然会产生错误。我如何解决这个问题?

+0

你的函数返回void '公共静态无效getOperator()' – Scony

+0

公共静态无效getOperator(){} ...你期待它返回什么? – JoeC

+0

代替退货。您目前正在返回一个不是字符的数字; '返回'';' –

回答

1

尝试了这一点

public static char getOperator(String fileContent){ 

    int checkAdd = fileContent.indexOf('+'); 
    int checkMinus = fileContent.indexOf('-'); 
    int checkMulti = fileContent.indexOf('*'); 
    int checkDivi = fileContent.indexOf('/'); 

    if (checkAdd != -1){ 
     char operator = fileContent.charAt(checkAdd); 
     return operator; 
    } 
    else if (checkMinus != -1) { 
     char operator = fileContent.charAt(checkMinus); 
     return operator; 
    } 
    else if (checkMulti != -1) { 
     char operator = fileContent.charAt(checkMulti); 
     return operator; 
    } 
    else if (checkDivi != -1){ 
     char operator = fileContent.charAt(checkDivi); 
     return operator; 
    } 

    return ' '; 
} 
+0

这工作!很棒,谢谢。 – CherryBomb95

2

基本上,方法的签名...

public static void getOperator(){ 

指出,该方法不返回任何东西。

相反,它应该被宣布更像......

public static char getOperator(){ 
0

你回报的方法是无效的,不烧焦,所以你不能返回一个值。