显示多个数组变量

时间:2017-06-26 17:17:00

标签: java arrays loops delimiter

该程序获得2个团队和2个结果的用户输入,用“:”分隔符分隔它们,然后将它们存储在数组中,当用户输入单词“stop”时,它停止询问用户输入并且意味着显示匹配的结果和统计信息(尚未添加到代码中)。我遇到的问题是,如果我键入多行匹配结果然后键入'stop',它只显示返回控制台的第一行用户输入而不是其他任何一行?输入例子:“切尔西:阿森纳:2:1”。

public static final String SENTINEL = "stop";

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);

    String hometeam = new String();
    String awayteam = new String();
    String homescore = new String();
    String awayscore = new String();

    int result0;
    int result1;

    System.out.println("please enter match results:");

    // loop, wil ask for match results ( b < () )
    for (int b = 0; b < 100; b++) {

        String s = sc.nextLine();

        // stop command
        while (sc.hasNextLine()) { // better than the for loop

            String line = sc.nextLine();

            String results[] = s.split(" : "); // parse strings in between
                                                // the

            for (String temp : results) {
                hometeam = results[0];
                awayteam = results[1];
                homescore = results[2];
                awayscore = results[3];
            }

            // convert 'score' strings to int value.
            result0 = Integer.valueOf(results[2]);
            result1 = Integer.valueOf(results[3]);

            if ("stop".equals(line)) {
                System.out.println(Arrays.toString(results));
                return; // exit
            }

3 个答案:

答案 0 :(得分:0)

输出您输入的第一个结果的原因是因为$id = 1; $where = "Código = {$id}"; // Or use this way $where = "Código = 1"; $this->tableGateway->update($set, $where); 已分配给resultss.split(" : ")永远不会在外部for循环的第一次迭代中发生变化,因此s永远不会改变。您的s.split(" : ")始终保持第一个匹配结果!

您编写的代码非常错误。

首先,为什么在for循环中有一个while循环? for循环是多余的。

其次,你不能使用数组。试试results。数组无法动态更改其大小。

第三,我建议你为此创建一个类,以表示ArrayList

MatchResult

然后,您可以创建一个存储用户输入的class MatchResult { private String homeTeam; private String awayTeam; private int homeScore; private int awayScore; public String getHomeTeam() { return homeTeam; } public String getAwayTeam() { return awayTeam; } public int getHomeScore() { return homeScore; } public int getAwayScore() { return awayScore; } public MatchResult(String homeTeam, String awayTeam, int homeScore, int awayScore) { this.homeTeam = homeTeam; this.awayTeam = awayTeam; this.homeScore = homeScore; this.awayScore = awayScore; } @Override public String toString() { return "MatchResult{" + "homeTeam='" + homeTeam + '\'' + ", awayTeam='" + awayTeam + '\'' + ", homeScore=" + homeScore + ", awayScore=" + awayScore + '}'; } }

ArrayList<MatchResult>

答案 1 :(得分:0)

尝试添加另一个数组

title-4

然后将您的值输入数组。我正在使用b,因为这是循环中的int变量。我也投入+&#34; :&#34;

description-4

然后将打印更改为

title-3

我认为这应该有用,但我无法测试它。

description-3

答案 2 :(得分:0)

这是一个简单的循环,用于从用户获取所有数据,直到输入“stop”并显示输入的输出

{
    "name": "domji/collection-manager",
    "license": "proprietary",
    "type": "project",
    "autoload": {
        "files": ["app/AppKernel.php"],
        "psr-4": {
            "": "src/",
            "SymfonyStandard\\": "app/SymfonyStandard/"
        }
    },
    "require": {
        "php": ">=5.3.9",
        "symfony/symfony": "3.*",
        "symfony/console": "3.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^2.8",
        "symfony/polyfill-apcu": "^1.0",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "~3.0",
        "incenteev/composer-parameter-handler": "^2.0",
        "friendsofsymfony/user-bundle": "~2.0",
        "ornicar/gravatar-bundle" : "~1.0",
        "symfony/assetic-bundle": "^2.7",
        "imdbphp/imdbphp": "^5.2"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
        "post-root-package-install": [
            "SymfonyStandard\\Composer::hookRootPackageInstall"
        ],
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-var-dir": "var",
        "symfony-bin-dir": "bin",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        }
    }
}

输出

IN