java - java将结果打印成两列错误

标签 java output

正如标题所述,我想将一些内容打印到两列中,但我似乎遇到了错误。

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;
}

这是我的输出,我希望将值分成彼此相邻的两列

enter image description here

最佳答案

你其实算计太多了...

看一下这个方法:
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;

    }

您的表格将完成,无需

for(int i = 0; i < noOfHorseInList; i++)
        {

情况1中:

关于java - java将结果打印成两列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184392/

相关文章:

java - 在 Java 中实现图形

java - OS-X 和 IZPack : user friendly ways to run installer on OS-X computers with NO Java installed

java - 如果我覆盖 .equals(),是否需要覆盖 .hashCode(),即使我不使用 HashMap?

output - 如果 NMDS(纯素)收敛不可能,这是否会使输出变得无用?

java - System.out.println 它在 Intellij Idea 中写入哪里?

C++ 日期输出

r - R 的 GLMER 输出,.sig01、.sig02、.sig03 的含义

java - 无法通过 Codility GenomicRangeQuery 测试的性能

java - JAXB 和文档对象

c - 在 C 中快速打印出大量整数