Void methods cannot return a value

前端 未结 10 2301
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 01:38

I\'m following the CS106A lectures online. I\'m going through the code on Lecture 12, but it\'s giving me errors in Eclipse.

This is my code. It seems the error is beca

10条回答
  •  醉酒成梦
    2021-01-23 01:49

    You can't have nested method in java.

    toLower() method is inside main().

    separately use both method. void means there isn't any return from those method.

    You can try as follows

    public static void main(String[] args){
       char output=new CS106A().toLower('a'); // calling toLower() and take the
                                              // return value to output
    
    }
    
    
    public char toLower(char ch){
    if (ch >= 'A' && ch <= 'Z'){
        return ((ch - 'A') + 'a');
      }
     return ch;   
    }
    

提交回复
热议问题