0

到目前为止我的代码:

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方法?

4

1 回答 1

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

}
于 2013-05-08T13:25:32.397 回答