Make Main Method restart/refresh

2019-08-29 08:11发布

My code so far:

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
    }
  }
}

How would I make it so that instead of:

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

It will refresh or restart the main method?

标签: java
1条回答
We Are One
2楼-- · 2019-08-29 08:50

Wrap your code in a while loop which you leave when the user chooses the exit command:

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")) ...
   } 

}
查看更多
登录 后发表回答