Java 从方法返回字符

Java 从方法返回字符,java,eclipse,methods,char,return,Java,Eclipse,Methods,Char,Return,我不知道出了什么问题,但是我不能让这个方法返回我想要的字符。我只是不断地在“return”一词下面出现一个以红线形式出现的错误 这是我的方法 public static char getOperator(String fileContent){ int checkAdd = fileContent.indexOf('+'); int checkMinus = fileContent.indexOf('-'); int checkMulti = fileContent.

我不知道出了什么问题,但是我不能让这个方法返回我想要的字符。我只是不断地在“return”一词下面出现一个以红线形式出现的错误

这是我的方法

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;


    }

好的,很抱歉,我在eclipse中一直在处理这个问题,没有发现这个问题。但是,现在ifs中的return语句仍然会产生错误。我如何解决这个问题?

基本上,方法签名

public static void getOperator(){
声明该方法不返回任何内容

相反,它应该被声明为更像

public static char getOperator(){

您的返回方法是void,而不是char,因此无法返回值。

请尝试此方法

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 ' ';
}

函数返回void“public static void getOperator()”public static void getOperator(){}。。。你希望它能退货吗?更换退货。您当前返回的是一个数字,而不是字符<代码>返回“”我仍然在if中得到返回错误statements@CherryBomb95看看我的答案。