如何从类内部调用main
我对 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();
}
}
我如何调用 main 以便代码在获得一个输入后不会退出?
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我注意到您试图重复执行
main
方法中的操作。您可以重复调用某些内容,而不是调用 main 方法(这被认为是错误的设计决策)。
如果您想重复执行某些操作,可以使用以下形式:
如果您应该停止执行循环,则
shouldEndCheck
方法将返回 true。如果您想检查输入是否有效,可以使用以下形式:
readInput
返回用户提供的String
(也可以更简单),validInput
是一个布尔方法,如果您认为输入有效,则返回true
,processInput
是您选择对有效输入执行的操作。我希望这有帮助。
I noticed you were trying to repeatedly do what is in the
main
method.Rather than calling the
main
method, which is something that is regarded as a bad design decision, you can call something repetitively.If you want to do something repetitively, you can use this form:
where the
shouldEndCheck
method returns true if you should stop doing the looping.If you want to check for valid input, you can use this form:
readInput
returns aString
provided by the user (it could be something simpler),validInput
is a boolean method and returnstrue
if the input is considered valid by you,and
processInput
is what you choose to do with a valid input.I hope this helps.
不,你不能以这种方式调用 main 。在输入读数中使用带有某种形式的终止条件的 while 循环。
No you cant invoke main this way. Use a while loop with some form of terminal condition in the input reading.
为什么要调用main?从你的开始方法? !!这将以无限递归调用结束。 Main将调用start,start将调用main
不过,如果您坚持这样做,您可以执行以下操作。
查看 start 方法签名如何变化以及我们如何将 null 传递给 main。
在您的情况下,除非您编写特殊的处理代码来在输入“q”或其他内容时执行“system.exit”,否则该程序将永远不会结束。
建议
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.
Suggested
这是另一个示例,演示了一些良好的类设计实践
类构造函数
使用 main 方法实例化类
使用布尔条件循环,以及有关如何结束循环的语句
Here is another example that demonstrates a couple good class design practices
A class constructor
Use of the main method to instantiate the class
Use of a boolean conditional loop, with a statement about how to end the loop