这是我的终端项目,在终端内,我可以键入“ create”,这将带我进入创建提示,在这里可以创建程序。我现在的问题是我无法回到Main类(可以在其中选择要运行的命令)。我有尝试使用System.exit(0)的想法;但是,正如我没有意识到的那样,它只是杀死了整个程序。如果有人可以帮助我,则下面是我的文件。如果需要,我可以发布任何其他文件。

import java.util.*;
import java.io.*;

public class commandCreate {
    boolean _active = true;
   String _username = System.getProperty("user.name").toLowerCase();
   String _os = System.getProperty("os.name").trim().toLowerCase();
   String fileName, create, option;

    public commandCreate() {
        try {
         while(_active) {
            System.out.print(_username + "@" + _os + ":~/create$ ");
            Scanner kbd = new Scanner(System.in);
                String userLine = kbd.nextLine();

            if(java.util.regex.Pattern.matches(".*\\S\\s+\\S.*", userLine)) {
                    Scanner read = new Scanner(userLine);
                        option = read.next();
                        fileName = read.next();
            }

            FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java"));

            if(userLine.equals(option + " " + fileName)) {
                if(option.equals("-a")) {
                    // Option = -a, creates standard file with main class.
                    create.write("public class " + fileName + " {\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      System.out.println(\"Welcome to your new program!\");\n");
                    create.write("  }\n");
                    create.write("}");
                } else if(option.equals("-c")) {
                    // Option = -c , creates standard file with overloaded constructor & main class.
                    create.write("public class " + fileName + " {\n");
                    create.write("  public " + fileName + "() {\n");
                    create.write("      System.out.println(\"Welcome to your new program!\");\n");
                    create.write("  }\n");
                    create.write("\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      new " + fileName + "();\n");
                    create.write("  }\n");
                    create.write("}");
                } else if(option.equals("-j")) {
                    // Option = -j, creates GUI within constructor w/ single JLabel.
                    create.write("import javax.swing.*;\n");
                    create.write("import java.awt.*;\n");
                    create.write("import java.awt.event.*;\n");
                    create.write("\n");
                    create.write("public class " + fileName + " extends JFrame {\n");
                    create.write("  private static final int HEIGHT = 50;\n");
                    create.write("  private static final int WIDTH = 400;\n");
                    create.write("\n");
                    create.write("  private JLabel welcomeJ;\n");
                    create.write("\n");
                    create.write("  public " + fileName + "() {\n");
                    create.write("    super(\"Welcome to your program - " + fileName + "\");\n");
                    create.write("      Container pane = getContentPane();\n");
                    create.write("    setLayout(new FlowLayout());\n");
                    create.write("\n");
                    create.write("      welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n");
                    create.write("\n");
                    create.write("      pane.add(welcomeJ);\n");
                    create.write("\n");
                    create.write("     setSize(WIDTH, HEIGHT);\n");
                    create.write("     setVisible(true);\n");
                    create.write("     setResizable(false);\n");
                    create.write("     setDefaultCloseOperation(EXIT_ON_CLOSE);\n");
                    create.write("  }\n");
                    create.write("\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      new " + fileName + "();\n");
                    create.write("  }\n");
                    create.write("}");
                }
            } else if(userLine.equalsIgnoreCase("help")) {
                System.out.println("Commands");
                System.out.println("  Syntax: [-option] [filename]");
                System.out.println("      -a [filename]      [Program: main class]");
                System.out.println("      -c [filename]      [Program: overloaded constructor, main class]");
                System.out.println("      -j [filename]      [Program: GUI: overloaded constructor, main class]");
            } else if(userLine.equalsIgnoreCase("exit")) {
                System.exit(0);
            } else {
                System.out.println("Error in syntax. Please review the \"help\" menu");
            }
            create.close();
         }
        } catch(IOException e) {
            System.out.println("There was an error: " + e);
        } catch(InputMismatchException ex) {
            System.out.println("There was an error: " + ex);
        }
    }

    public static void main(String[] args) {
        new commandCreate();
    }
}

最佳答案

简单的答案是让commandCreate构造函数返回,或引发/传播异常。确实,我认为如果用户输入EOF,这已经发生。

(您的代码还有很多其他错误,但是如果您自己弄清楚这可能会更好。我会指出,“ commandCreate”或“ CommandCreate”对于类名来说确实是一个糟糕的选择。一个类名称通常是一个名词。)

10-08 13:49