【问题标题】:Returning to main method? [closed]返回主方法? [关闭]
【发布时间】:2014-01-30 22:52:11
【问题描述】:

我的代码在如何像其他人一样执行 main 方法时遇到了一些问题。 这是我需要的示例:

(...)
public static void main (String[] args)
{
    hi();
    System.out.println("cake time!");
}
public static void hi ()
{
    main();
}

我想知道如何运行那个“蛋糕时间!”,替换那个“main();”以一种很好的方式,与“hi();”的目的相同

感谢阅读!

【问题讨论】:

    标签: java methods return main


    【解决方案1】:

    你不打电话给main()。当你的应用程序启动时,它会为你调用,当你到达它的末尾时,你的应用程序就完成了。打电话给main() 是没有意义的。

    对于你需要的,你有一个可用的方法(你甚至在你的主题中使用它):

    public static void main (String[] args)
    {
        hi();
        // The return from hi() executes the next line of code
        System.out.println("cake time!");
    }
    public static void hi ()
    {
        // Do whatever hi() does here. When it's done, 
        // you automatically go back to the line after the one
        // that called you. (You "return" to the next line).
        // If the function returns a value (isn't void), you use 
        // the keyword 'return'.
    }     
    

    【讨论】:

    • return 也不是必须的,它会自动转到 main。
    • @VenkateshAchanta:是的。澄清了我的意思。谢谢。
    • 我没听懂...可能是因为我的英语水平很高。我想在我的程序中创建一个“循环”,但是无论是否返回它都不起作用:/。无论如何,非常感谢您的支持。
    • 等等,同一个答案我看了8遍,终于明白了!感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2017-10-02
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多