1

私は Java でコマンドライン マトリックス操作ツールを作成していますが、コンソール入力を通じて Java ステートメントを実行できるかどうか疑問に思っていました。

アプリケーションの残りの部分で使用しているため、 java.util.Scannerオブジェクトを使用することを考えていましたが、どのような解決策も受け入れます。

これが私のツールのアプリケーション クラスのコピーです。

package projects.matrix.main;
import static java.lang.System.*;
import java.util.Scanner;
import projects.matrix.util.MatrixTool;
/**
 * MATRIX :: Application class for the matrix toolset. 
 * @author  toner
 * @version May 28 2015
 * @since   1.8
 **/
public class MarixApp {
    public static void main (String [] args) {
    out.println("****************************** START*****************"  
     + "*************\n");
        runCommandLine();
        out.println("******************************  END  *****************" 
         + "*************\n");
    }

    /**
     * MATRIX.MAIN :: runCommandLine runs a loop command line 
     * @param   none
     * @return  none
     **/
    public static void runCommandLine () {
        // method vars
        Scanner scanner = new Scanner(in);
        MatrixTool matrixtool = new MatrixTool();
        int[][] matrix1 = new int[0][0];
        int[][] matrix2 = new int[0][0];
        int[][] resultmatrix = new int[0][0];
        String command = "";
        int executerret = 0;

        // welcome prints
        out.println("[!] welcome to toner's matrix tool command-line");
        out.println("[!] enter 'HELP' to view available commands\n");

        // commmand-line loop
        do {
            out.print(" [?] >> ");
            command = scanner.nextLine();
            executerret = executecmd(command);
        } while (executerret != -1);
    }

    /**
     * MATRIX.MAIN :: executecmd executes the command passed by runCommandLine
     * @param   cmd         : String
     * @return  returncode  : int
     **/
    public static int executecmd (String cmd) {
        // method vars
        Scanner scanner = new Scanner(in);
        MatrixTool matrixtool = new MatrixTool();
        int returncode = 0;

        // command executer
        switch (cmd) {
            case "HELP" : 
            case "help" : 
                out.println("\n"
                    + "  [%]    ADD        DIVIDE     HELP       "
                    + "MULTIPLY   PRINT"
                    + "  [%]    RUNJAVA    SUBSTRACT  SETMTRX    "
                    + "SETMTRX1   SETMTRX2"
                    + "  [%]    TRANSPOSE  RUNOPS     RESET      "
                    + "EXIT\n");
                break;
            // rest of commands go here
        }
    }
}

よろしく。

4

2 に答える 2

2

Groovy の使用について考えたことはありますか? これは JVM ベースの動的言語であり、コードを動的に実行できることを意味します。コードは文字列として提供されます。構文的には Java に非常に近いです。コード例は次のようになります。

new GroovyShell().evaluate("println 'hello'")
于 2015-05-29T15:10:34.083 に答える
1

コンソール入力を Java ステートメントとして実行することで、コンソールから正確な入力を取得してアプリケーション内で実行することを意味する場合は、それらをコンパイルして実行する必要があります。これは、コンパイラ APIを使用して行うことができます。

于 2015-05-29T15:14:00.723 に答える