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

查看:146
本文介绍了如何在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")) { 

第二个问题是解析另一个。

The second problem is to parse off the ANOTHER.

args[x].split("=") 

是开始的地方。

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

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