【问题标题】:Make Main Method restart/refresh使主方法重新启动/刷新
【发布时间】:2013-05-08 21:30:35
【问题描述】:

到目前为止我的代码:

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

【问题讨论】:

    标签: java


    【解决方案1】:

    将您的代码包装在一个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")) ...
       } 
    
    }
    

    【讨论】:

    • 你能不能用正确的括号把它放到 main 方法中?我被括号弄混了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2020-02-13
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多