Java void 方法不能返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20490874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Void methods cannot return a value
提问by Aaron Ausmus
I'm following the CS106A lectures online. I'm going through the code on Lecture 12, but it's giving me errors in Eclipse.
我正在在线关注 CS106A 讲座。我正在阅读第 12 课中的代码,但它在 Eclipse 中给了我错误。
This is my code. It seems the error is because of the word void in my main
method. I tried deleting the main method, but of course Java can't run without it.
这是我的代码。似乎错误是因为我的main
方法中的单词 void 。我尝试删除 main 方法,但当然没有它 Java 就无法运行。
I'm a newbie and no one has explained what the String[] args
thing really means, but I've been told to just ignore it and use it. I'd appreciate if someone could explain that to me as well.
我是一个新手,没有人解释这String[] args
件事的真正含义,但我被告知忽略它并使用它。如果有人也能向我解释这一点,我将不胜感激。
This errors also comes up on the 'toLower' method; no idea what it means: Illegal modifier for parameter toLower; only final is permitted
这个错误也出现在 'toLower' 方法上;不知道这是什么意思:参数 toLower 的非法修饰符;只允许final
(if it helps; the point of the code is to convert an uppercase letter to a lowercase one)
(如果有帮助;代码的重点是将大写字母转换为小写字母)
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;
}
}
Thanks
谢谢
采纳答案by Steve P.
You should be defining your method outside of main, like:
您应该在 main 之外定义您的方法,例如:
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.
Java 不支持嵌套方法;但是,有一些解决方法,但它们不是您想要的。
As for your question about args
, it is simply an array of Strings
that correspond to command line arguments. Consider the following:
至于你关于 的问题args
,它只是一个Strings
对应于命令行参数的数组。考虑以下:
public static void main(String... args) //alternative to String[] args
{
for (String argument: args)
{
System.out.println(argument);
}
}
Executing via java YourClass Hello, World!
执行通过 java YourClass Hello, World!
Will print
会打印
Hello,
Word!
你好,
字!
回答by BobTheBuilder
You cannot declare a method (toLower
) inside another method (main
).
您不能toLower
在另一个方法 ( main
) 中声明一个方法 ( )。
回答by Ruchira Gayan Ranaweera
You can't have nested method in java.
java中不能有嵌套方法。
toLower()
method is inside main()
.
toLower()
方法在里面main()
。
separately use both method. void
means there isn't any return from those method.
分别使用这两种方法。void
意味着这些方法没有任何回报。
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;
}
回答by Deepak
nested method defining is not allowed in java .You have defined your method toLower()
inside main
method
java中不允许定义嵌套方法。你已经toLower()
在main
方法中定义了你的方法
回答by Gourav
Yes void return type can not return any value.
是的 void 返回类型不能返回任何值。
It will be better if you create a separate function for this process which will return some value and call it from main().
如果您为此进程创建一个单独的函数会更好,该函数将返回一些值并从 main() 调用它。
public class Test
{
public static void main(String[] args)
{
String[] a = testMethod();
}
public String[] testMethod()
{
.....
.....
return xx;
}
}
Hope it will help you.
希望它会帮助你。
Thanks
谢谢
回答by Aniket Thakur
You cannot have such nested methods in Java. CS106A is a class. main()
and toLower()
are two methods of it. Write them separately.
在 Java 中不能有这样的嵌套方法。CS106A是一类。main()
并且toLower()
是它的两种方法。分别写出来。
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.
至于String[] args
在 main() 方法参数中,int arc, char **argv
如果您以前学过它,它类似于在 C 中所说的。所以基本上 args 是一个包含所有命令行参数的数组。
回答by Funkotron_King
You need to declare your method outside of the main
您需要在 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)
字符串 args 位是这样,当您通过命令行运行它时,您可以发送值(作为字符串)
>java myprogram var1 var2 ....
回答by Sangam Belose
Because You have added ; at the end of an method
因为你添加了; 在方法结束时
corrected Code:
更正的代码:
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;
}
}
Please Read how to write Methods in java on any java website
请阅读如何在任何 java 网站上用 java 编写方法
回答by Muhammad Essa
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'));
}
}
Hope this will help you.
希望这会帮助你。
回答by Tushar Goel
string[] args in main method is used to follow the prototype defined by the technology. If you dont used it then JVM will considered it as a simple method. Also as this is prototype then remember technology used 0 byte of an array. for ex:
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 for above will give:
上面的 O/p 将给出:
I am main method without arg
MainDemo
Regarding your code, you cannot define/declare any method inside main method. Use it outside main method but within class.
关于您的代码,您不能在 main 方法中定义/声明任何方法。在 main 方法之外但在类内使用它。