java 如何从班级内部调用main
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7410972/
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
How to call main from inside class
提问by Icallitvera
I and really new to Java and I'm sure that there is a way to do this so I'm going to ask: Can you call the main method from the class?
我真的是 Java 的新手,我确信有一种方法可以做到这一点,所以我要问:你能从类中调用 main 方法吗?
import java.io.*;
public class Chemicalcommandline {
public void start() {
String instructions = "This program "
+ ""
+ ""
+ ""
+ "";
System.out.println();
System.out.println("Chemical Sign: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String chemical = null;
try {
chemical = reader.readLine();
} catch(IOException ioe) {
System.out.println("Error");
}
//start very long if
if (chemical.equals("Ca")) {
System.out.println("Calcium");
}
main();
}
public static void main(String[] args) {
Chemicalcommandline client = new Chemicalcommandline();
client.start();
}
}
How could I call the main so that the code does not quit after getting one input?
我如何调用 main 以便代码在获得一个输入后不会退出?
回答by Kaushik Shankar
I noticed you were trying to repeatedly do what is in the main
method.
我注意到你试图重复执行main
方法中的内容。
Rather than calling the main
method, which is something that is regarded as a bad design decision, you can call something repetitively.
main
您可以重复调用某些内容,而不是调用方法(这被认为是一个糟糕的设计决策)。
If you want to do something repetitively, you can use this form:
如果你想重复做某事,你可以使用这种形式:
public static void main(String[] args) {
boolean endCondition = false;
while(!endCondition) {
Chemicalcommndline.start();
endCondition = shouldEndCheck();
}
}
where the shouldEndCheck
method returns true if you should stop doing the looping.
shouldEndCheck
如果您应该停止循环,则该方法返回 true。
If you want to check for valid input, you can use this form:
如果要检查有效输入,可以使用此表单:
public static void main(String[] args) {
String input = "";
do {
input = readInput();
} while (!validInput(input));
processInput(input);
}
readInput
returns a String
provided by the user (it could be something simpler),
validInput
is a boolean method and returns true
if the input is considered valid by you,
and processInput
is what you choose to do with a valid input.
readInput
返回一个String
由用户提供的(它可能更简单),
validInput
是一个布尔方法,true
如果输入被你认为是有效的,processInput
则返回,并且是你选择对有效输入执行的操作。
I hope this helps.
我希望这有帮助。
回答by CoolBeans
No you cant invoke main this way. Use a while loop with some form of terminal condition in the input reading.
不,您不能以这种方式调用 main。在输入读数中使用带有某种形式的终端条件的 while 循环。
回答by Sap
Why do you want to call main? from your start method? !! this will end in an infinite recursive calls. Main will call start and start will call main Though if you insist in doing it you can do the following. See how the start method signature changes and how we pass null to main. In your case this program will never end unless you write special handling code to do "system.exit" on entering "q" or something.
为什么要调用main?从你的开始方法?!! 这将以无限递归调用结束。Main 将调用 start , start 将调用 main 虽然如果您坚持这样做,您可以执行以下操作。看看 start 方法签名如何变化以及我们如何将 null 传递给 main。在您的情况下,除非您编写特殊处理代码以在输入“q”或其他内容时执行“system.exit”,否则该程序将永远不会结束。
import java.io.*;
public class Chemicalcommandline {
public static void start() {
String instructions = "This program "
+ ""
+ ""
+ ""
+ "";
System.out.println();
System.out.println("Chemical Sign: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String chemical = null;
try {
chemical = reader.readLine();
} catch(IOException ioe) {
System.out.println("Error");
}
//start crazy long if
if (chemical.equals("Ca")) {
System.out.println("Calcium");
}
main(null);
}
public static void main(String[] args) {
Chemicalcommandline.start();
}
}
Suggested
建议
import java.io.*;
public class Chemicalcommandline {
public static void start() {
String instructions = "This program "
+ ""
+ ""
+ ""
+ "";
System.out.println(instructions);
System.out.println("Chemical Sign: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String chemical = null;
try {
chemical = reader.readLine();
if(chemical.equals("exit")){
System.exit(0);
}
} catch(IOException ioe) {
System.out.println("Error");
}
//start crazy long if
if (chemical.equals("Ca")) {
System.out.println("Calcium");
}
main(null);
}
public static void main(String[] args) {
//Chemicalcommandline client = new Chemicalcommandline();
Chemicalcommandline.start();
}
}
回答by jamesTheProgrammer
Here is another example that demonstrates a couple good class design practices
这是另一个示例,它展示了一些良好的类设计实践
A class constructor
类构造函数
Use of the main method to instantiate the class
使用main方法实例化类
Use of a boolean conditional loop, with a statement about how to end the loop
使用布尔条件循环,并说明如何结束循环
import java.io.*;
public class Chemicalcommandline {
//class constructor
public Chemicalcommandline(){
start();
}
public static void start() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String chemical = null;
Boolean done = false;
while(!done){
String instructions = "This program "
+ ""
+ ""
+ ""
+ "";
System.out.println(instructions);
System.out.println("Chemical Sign: , enter exit to end");
try {
chemical = reader.readLine();
if(chemical.equals("exit")){
done = true;
}
}catch(IOException ioe) {
System.out.println("Error");
}
if (chemical.equals("Ca")) {
System.out.println("Calcium");
}else{
System.out.println("not sure what that is...");
}
}// end while
}//end method start
public static void main(String[] args){
new Chemicalcommandline();
}
}//end class Chemicalcommandline