正如标题所述,我希望将内容打印成两列,但我似乎遇到了错误。
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;
}
这是我的输出,我希望将值分成两列彼此
答案 0 :(得分:0)
在相同的功能中添加马的名称和时间。
使用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);
答案 1 :(得分:0)
你实际上计算得太多了......
看看这个方法:
终结者(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 中的
答案 2 :(得分:-1)
如何将for循环中的以下行更改为 -
graph=None