Void methods cannot return a value

前端 未结 10 2184
爱一瞬间的悲伤
爱一瞬间的悲伤 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:59

    You cannot declare a method (toLower) inside another method (main).

    0 讨论(0)
  • 2021-01-23 02:01

    You should be defining your method outside of main, like:

    public class YourClass
    {
        public static void main(String... args)
        {
    
        }
    
        public char yourMethod()
        {
             //...
        }
    }
    

    Java does not support nested methods; however, there are workarounds, but they are not what you're looking for.

    As for your question about args, it is simply an array of Strings that correspond to command line arguments. Consider the following:

    public static void main(String... args) //alternative to String[] args
    {
        for (String argument: args)
        {
            System.out.println(argument);
        }
    }
    

    Executing via java YourClass Hello, World!

    Will print

    Hello,
    Word!

    0 讨论(0)
  • 2021-01-23 02:03

    You cannot have such nested methods in Java. CS106A is a class. main() and toLower() are two methods of it. Write them separately.

    As for String[] args in the main() method argument it is similar to saying int arc, char **argv in C if you have learned it before. So basically args is an array where all the command line arguments go.

    0 讨论(0)
  • 2021-01-23 02:05

    You need to declare your method outside of the main

    public class YourClass
    {
        public static void main(String... args)
        {
    
        }
    
        public char yourMethod()
        {
    
        }
    }
    

    the string args bit is so when you run it through command line you can send values (as strings)

    >java myprogram var1 var2 ....
    
    0 讨论(0)
提交回复
热议问题