首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以语句形式运行Java控制台输入

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

Stack Overflow用户
提问于 2015-05-29 15:00:45
回答 2查看 226关注 0票数 1

我正在用Java编写命令行矩阵操作工具,并想知道是否可以通过控制台输入运行Java语句。

我考虑使用java.util.Scanner对象,因为这是我在应用程序的其余部分使用的对象,但我对任何解决方案都是开放的。

下面是我的工具的应用程序类的副本,所以您可以看到我的意思:

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

致以问候。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-29 15:14:00

如果将控制台输入作为java语句运行,意味着从控制台获取准确的输入并在应用程序中运行,则需要编译并运行它们。您可以使用编译器API完成此操作。

票数 1
EN

Stack Overflow用户

发布于 2015-05-29 15:10:34

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

代码语言:javascript
复制
new GroovyShell().evaluate("println 'hello'")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30532892

复制
相关文章

相似问题

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