正如标题所述,我想将一些内容打印到两列中,但我似乎遇到了错误。
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;
}
这是我的输出,我希望将值分成彼此相邻的两列
最佳答案
你其实算计太多了...
看一下这个方法:
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/