java 将结果打印成两列错误

Printing result into two columns error for java

正如标题所述,我想将一些内容打印成两列,但我似乎 运行 出错了。

int noOfHorseInList = 7;
        String horseName[] = {"Blitz", "Sparky", "Sandy", "Rolo", "Beano", 

"Flash", "", "", ""};
        int horseAge[] = {2, 4, 1, 4, 4, 2, 0, 0, 0};
        int timeTakenInSeconds[] = {600, -1, 500, 430, 412, -1, 0, 0, 0};

    int option = 0;

    System.out.println("Please choose from the following selection");
    System.out.println("1. Show horses that finished\n2. Show horse details\n3. Adding a Late Entrant");
    option = kb.nextInt();
    kb.hasNextLine();

    switch(option)
    {
    case 1:
        //Finishers
        String showHorseThatFinished = "";
        showHorseThatFinished = finishers(noOfHorseInList, horseName, timeTakenInSeconds);

        String timeOfHorse = "";

        for(int i = 0; i < noOfHorseInList; i++)
        {
            if(timeTakenInSeconds[i] > -1)
            {
                timeOfHorse = timeOfHorse + "\n" + timeTakenInSeconds[i];
            }
        }


        System.out.println("Horses Name\tFinishing Time");
        System.out.println(showHorseThatFinished + "\t" + timeOfHorse);



        break;

    case 2:
        //Horse details





        break;

    case 3:
        //Adding Late Entrant



        break;

    }

}

public static String finishers(int noOfHorseInList,String horseName[],int timeTakenInSeconds[])
{
    String showHorseThatFinished = "";

    for(int i = 0; i < noOfHorseInList; i++)
    {
        if(timeTakenInSeconds[i] > -1)
        {
            showHorseThatFinished = showHorseThatFinished + "\n" + horseName[i];
        }
    }


    return showHorseThatFinished;
}

这是我的输出,我希望将值分成两列并排

如何将 for 循环中的以下行更改为 -

timeOfHorse = timeOfHorse + "\t" + timeTakenInSeconds[i];

在同一函数中附加马名和时间。

使用 StringBuffer 进行追加。效率更高。

public static String finishers(int noOfHorseInList,String   horseName[],int timeTakenInSeconds[]) {
StringBuffer showHorseThatFinished = new StringBuffer();

for(int i = 0; i < noOfHorseInList; i++) {
        // You need all horses to be written. Mark somehow that their time is erroneous
        String time = "ERROR";    

        if (timeTakenInSeconds[i] > -1) {
              time = "" + timeTakenInSeconds[i];
        }

        showHorseThatFinished.append(horseName[i]);
        showHorseThatFinished.append("\t");
        showHorseThatFinished.append(time);
        showHorseThatFinished.append("\n");
    }
}

    return showHorseThatFinished.toString();
}

不需要这段代码了

    String timeOfHorse = "";

    for(int i = 0; i < noOfHorseInList; i++)
    {
        if(timeTakenInSeconds[i] > -1)
        {
            timeOfHorse = timeOfHorse + "\n" + timeTakenInSeconds[i];
        }
    }

刚刚

  System.out.println(showHorseThatFinished);

你其实算多了...

看看这个方法:
finishers(int noOfHorseInList, String horseName[], int timeTakenInSeconds[])

修改为:

public static String finishers(int noOfHorseInList, String horseName[], int timeTakenInSeconds[]) {
        String showHorseThatFinished = "";

        for (int i = 0; i < noOfHorseInList; i++) {
            if (timeTakenInSeconds[i] > -1) {
                showHorseThatFinished = showHorseThatFinished + "\n" + horseName[i] + "\t\t\t" + timeTakenInSeconds[i];
            }
        }
        return showHorseThatFinished;

    }

并且您的 table 将在不需要

的情况下完成
for(int i = 0; i < noOfHorseInList; i++)
        {

案例 1 中: