使主方法重新启动/刷新

时间:2013-05-08 13:22:22

标签: java

到目前为止我的代码:

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
}

它会刷新或重启主方法吗?

1 个答案:

答案 0 :(得分:3)

将代码包装在用户选择退出命令时留下的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")) ...
   } 

}