如何在 Java 中使用带有值的终端参数? [英] How to use terminal arguments with values in Java?

查看:30
本文介绍了如何在 Java 中使用带有值的终端参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的学校项目,我正在为 iPhone 创建一个类似 Bad Apples 的游戏(不是我个人的选择,但这不是问题).

For my school project I am creating a game like Bad Apples for iPhone (not my personal choice but it isn't the problem).

游戏需要有两个版本,第一个在控制台,第二个在 JavaFX.但我想更进一步.我想添加用户在启动游戏时可以添加到终端的参数,例如

The game needs to have two versions, the first one in console and the second one in JavaFX. But I wanted to go a little further with that. I want to add arguments that the user can add to the terminal when launching the game, for example

java -jar BadApplesClone.jar --height=10 --width=10 --numPieces=5

java -jar BadApplesClone.jar --height=10 --width=10 --numPieces=5

然后我将处理所有引入的值并更改变量.

And then I will handle all the values introduced and change the variables.

我正在使用 OpenJDK6,所以我现在是这样做的:

I am using OpenJDK6, and so I am doing like this for now :

    for (int i=0; i<args.length; i++)
    {
        if (args[i].equals("--help"))
            throw new UnsupportedOperationException("Not yet implemented");

        if (args[i].equals("--largura"))
            throw new UnsupportedOperationException("Not yet implemented");

        if (args[i].equals("--altura"))
            throw new UnsupportedOperationException("Not yet implemented");

        if (args[i].equals("--pecas_inicio"))
            throw new UnsupportedOperationException("Not yet implemented");

        if (args[i].equals("--javafx"))
        {
            JavaFX javaFX = new JavaFX(ALTURA, LARGURA, PECAS_INICIO);
            javaFX.initJogo();
        }
    }

但我不知道如何处理像 --width=10.. 这样的值.我想到了一个枚举,但我真的不知道该怎么做.

But I don't know how to handle the values like --width=10.. I have thought of an enum, but I don't really know how to do that.

谁能解释一下实现这一目标的方法?

Can anyone explain me a way to achieve this?

推荐答案

回答你实际提出的问题...

To answer the question you actually asked ...

您的 args 数组的某些元素采用--SOMETHING=ANOTHER"形式.

Some of the elements of your args array are of the form "--SOMETHING=ANOTHER".

所以,你首先需要的是:

So, first thing you need is:

if(args[x].startsWith("--SOMETHING")) { 

第二个问题是解析ANOTHER.

The second problem is to parse off the ANOTHER.

args[x].split("=") 

是开始的地方.

这篇关于如何在 Java 中使用带有值的终端参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆