【问题标题】:Void methods cannot return a valuevoid 方法不能返回值
【发布时间】:2013-12-10 09:42:13
【问题描述】:

我正在在线关注 CS106A 讲座。我正在阅读第 12 课的代码,但它在 Eclipse 中给了我错误。

这是我的代码。似乎错误是因为我的 main 方法中的单词 void。我尝试删除 main 方法,但是没有它当然 Java 无法运行。

我是新手,没有人解释过 String[] args 的真正含义,但有人告诉我忽略它并使用它。如果有人也可以向我解释这一点,我将不胜感激。

这个错误也出现在'toLower'方法上;不知道是什么意思: 参数 toLower 的非法修饰符;只允许决赛

(如果有帮助;代码的重点是将大写字母转换为小写字母)

public class CS106A {

    public static void main(String[] args){

        public char toLower(char ch);
            if (ch >= 'A' && ch <= 'Z'){
                return ((ch - 'A') + 'a');
        }
        return ch;      
    }

}

谢谢

【问题讨论】:

  • 50 分钟前讨论了类似类型的问题 stackoverflow.com/questions/20489856/…
  • String[] args 是您传递给程序的命令行参数的数组。这很可能是空的,因为您没有通过任何。

标签: java methods void


【解决方案1】:

您应该在 main 之外定义您的方法,例如:

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

    }

    public char yourMethod()
    {
         //...
    }
}

Java 不支持嵌套方法;然而,有workarounds,但它们不是你要找的。​​p>

至于你关于args 的问题,它只是一个Strings 的数组,对应于命令行参数。考虑以下几点:

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

通过java YourClass Hello, World!执行

将打印

你好,
字!

【讨论】:

    【解决方案2】:

    您不能在另一个方法 (main) 中声明一个方法 (toLower)。

    【讨论】:

      【解决方案3】:

      是的void返回类型不能返回任何值。

      如果您为此进程创建一个单独的函数会更好,该函数将返回一些值并从 main() 调用它。

      public class Test
      {
          public static void main(String[] args)
          {
              String[] a = testMethod();
          }
      
          public String[] testMethod()
          {
              .....
              .....
              return xx;
          }
      }
      

      希望对你有帮助。

      谢谢

      【讨论】:

        【解决方案4】:

        你需要在main之外声明你的方法

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

        字符串 args 位是这样,当您通过命令行运行它时,您可以发送值(作为字符串)

        >java myprogram var1 var2 ....
        

        【讨论】:

          【解决方案5】:

          因为你添加了 ;在方法结束时

          更正的代码:

          public class CS106A {
          
          public static void main(String[] args){
          Char char=new CS106A.toLower('s');
          System.out.println(char);
          }
          
          public char toLower(char ch)
          {
              if (ch >= 'A' && ch <= 'Z'){
                  return ((ch - 'A') + 'a');
              }
              return ch;      
          }
          }
          

          请在任何java网站上阅读如何用java编写方法

          【讨论】:

            【解决方案6】:

            Java 中不能有嵌套方法。

            toLower() 方法在 main() 内部。

            分别使用这两种方法。 void 表示这些方法没有任何返回。

            你可以尝试如下

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

            【讨论】:

              【解决方案7】:

              在java中不允许嵌套方法定义。你已经在main方法中定义了你的方法toLower()

              【讨论】:

                【解决方案8】:

                Java 中不能有这样的嵌套方法。 CS106A是一类。 main()toLower() 是它的两种方法。分别写出来。

                至于 main() 方法参数中的String[] args,如果你以前学过的话,它类似于在 C 中说 int arc, char **argv。所以基本上 args 是一个包含所有命令行参数的数组。

                【讨论】:

                  【解决方案9】:
                  public class CS106A
                  {
                      public static char toLower(char ch)
                      {
                          if (ch >= 'A' && ch <= 'Z')
                          {
                              return ((ch - 'A') + 'a');
                          }
                      return ch;
                      }
                      public static void main(String[] args)
                      {
                  
                         System.Out.Println(toLower('A'));
                  
                      }
                  
                  }
                  

                  希望这会对你有所帮助。

                  【讨论】:

                    【解决方案10】:
                    main 方法中的

                    string[] args 用于遵循技术定义的原型。如果你不使用它,那么 JVM 会认为它是一种简单的方法。同样,由于这是原型,请记住使用数组的 0 字节的技术。例如:

                                   class MainDemo{
                                        public static void main(){
                                        System.out.println("I am main method without arg");
                                    }
                                    public static void main(String[] args){
                                            main();   //As method is static so no need to create an object
                                        System.out.println("MainDemo");
                                    }
                                    }
                    

                    上面的 O/p 将给出:

                     I am main method without arg 
                     MainDemo
                    

                    关于您的代码,您不能在 main 方法中定义/声明任何方法。在 main 方法之外但在类中使用它。

                    【讨论】:

                      猜你喜欢
                      • 2015-10-03
                      • 2015-04-30
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-11-25
                      • 2012-11-24
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-01-07
                      相关资源
                      最近更新 更多