首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使Main方法重新启动/刷新

使Main方法重新启动/刷新
EN

Stack Overflow用户
提问于 2013-05-08 21:22:22
回答 1查看 4.5K关注 0票数 0

到目前为止我的代码如下:

代码语言:javascript
复制
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
    }
  }
}

我如何才能做到这一点而不是:

代码语言:javascript
复制
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方法?

EN

回答 1

Stack Overflow用户

发布于 2013-05-08 21:25:32

将您的代码包装在一个while循环中,当用户选择exit命令时,您将离开该循环:

代码语言:javascript
复制
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")) ...
   } 

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16441869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档