以语句形式运行Java控制台输入

以语句形式运行Java控制台输入,java,java.util.scanner,console-input,Java,Java.util.scanner,Console Input,我正在用Java编写一个命令行矩阵操作工具,想知道是否可以通过控制台输入运行Java语句 我曾考虑使用java.util.Scanner对象,因为这是我在应用程序其余部分中使用的对象,但我对任何解决方案都持开放态度 这是我的工具的应用程序类的副本,因此您可以了解我的意思: package projects.matrix.main; import static java.lang.System.*; import java.util.Scanner; import projects.matrix.

我正在用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
        }
    }
}

问候

您考虑过使用Groovy吗?它是一种基于JVM的动态语言,这意味着您可以动态执行代码——以字符串形式出现的代码。从语法上讲,它非常接近Java。示例代码可能如下所示:

new GroovyShell().evaluate("println 'hello'")

如果将控制台输入作为java语句运行,您的意思是从控制台获取准确的输入并在应用程序中运行,那么您需要编译并运行它们。您可以使用

问题出在哪里?@singhakash还没有问题,我想以Java语句的形式运行控制台输入,但我不知道如何开始,也不知道从哪里开始。所以您基本上想要构建一个Java解释器,对吗?我认为使用Groovy(正如“defectus”所建议的)或另一种更适合于此的编程语言会容易得多。可能是Python。一个简单的建议是,不要使用ByTestStream提供
Scanner(包装类)
,而是尝试实现
Scanner
,以便通过使用
Scanner Scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)),以
缓冲区的形式提供输入
这有什么变化?你能解释一下用你自己的方式而不是使用包装器类的优点吗?看起来很有趣,我来试一试。我该怎么做呢?