您可以在MAIM中定义方法' public static void main(string [] args)' Java中的方法?
我是Java的新手,并且一直在Eclipse IDE中练习编程。我有以下代码:
public static void main(String[] args) {
public double absoluteValue(double n) {
if (n < 0) {
return -n;
} else {
return n;
}
}
System.out.println(absoluteValue(-5));
}
我尝试创建一个返回绝对值的方法absoluteValue()
。但是,Eclipse向我扔了很多错误。
当我定义 absoluteValue()
之外,我设法使我的代码工作正常静态双绝对值(double n))和在主方法中称其为。
但是我只是想知道为什么上述代码不起作用。您可以在主要方法中定义方法(如果是这样,应该)?
I'm new to Java and have been practising programming within the Eclipse IDE. I have the following code:
public static void main(String[] args) {
public double absoluteValue(double n) {
if (n < 0) {
return -n;
} else {
return n;
}
}
System.out.println(absoluteValue(-5));
}
I tried to create a method absoluteValue()
which returns absolute values. However, Eclipse threw a bunch of errors at me.
I managed to get my code to work when I defined absoluteValue()
outside of the main method (had to define it as public static double absoluteValue(double n)
though) and called it within the main method.
But I was just wondering why the above code doesn't work. Can you define methods within the main method (if so, should you)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java不允许嵌套这种方法。您可以在方法内有lambdas,但不能在方法内定义完整的方法。这是毕达斯塔斯人知道的重要区别。
Java does not allow nesting of methods of this sort. You can have lambdas inside of a method but you can't define a full method inside of a method. This is an important difference for Pythonistas to know.