gpt4 book ai didi

java - 使 Main 方法重新启动/刷新

转载 作者:行者123 更新时间:2023-12-01 23:49:56 25 4
gpt4 key购买 nike

到目前为止我的代码:

import java.util.*;
import java.util.Scanner.*;

public class Project{ // The Main Method
public static void main(String [] args){ // Creates the Main Method
System.out.println("Name a Method (Stability, efficiency ..)"); // Asks the user to select a method
Scanner scan = new Scanner(System.in); // Creates the Scanner
String splash = scan.nextLine(); // Transitions the user to the next line after choosing a method
if(splash.equals("efficiency")) // If users chooses Efficiency then it goes to the Efficiency method
{
efficiency(); // Calls the Efficiency method
}
if(splash.equals("Stability")) // If user chooses Stability then it goes to the Stability Method
{
stable(); // Calls the Stability method
}
else // What happens if the input wasnt recognized

{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}
}
}

我该如何做到这一点,而不是:

else // What happens if the input wasnt recognized 
{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}

会刷新或者重启main方法吗?

最佳答案

将代码包装在 while 循环中,当用户选择退出命令时留下该循环:

public static void main(String [] args){

while (true) {
System.out.println("Name a Method (Stability, efficiency ..)");
Scanner scan = new Scanner(System.in);
String splash = scan.nextLine();
if (splash.equals("exit")) {
break;
} // else if (splash.equals("efficiency")) ...
}

}

关于java - 使 Main 方法重新启动/刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com