2016-03-23 16 views
1

タイトルと同じように2つの列に何かを印刷したいが、エラーが発生しているようだ。 2つの列に結果を出力するエラー

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

+0

エラーは何ですか? –

+0

追加する必要はありませんが、イメージに表示されている出力を1つの列ではなく別の列に表示する必要があります。私ができない問題は、これを行う方法を理解するようです。 – Solus

+0

すべての馬を印刷するfinishers()を呼び出しています。次に、すべての時間を印刷するforループを実行します。同じ行に置いておきたい場合は、同じforループ内にすべて同時に印刷する必要があります。基本的にフィニッシャーの機能を取り除く –

答えて

0

...

はこれを見てとるもう

String timeOfHorse = ""; 

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

コードのこの部分のための必要はありません

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

方法:
フィニッシャー(int noOfHo rseInList、文字列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:

+0

ありがとうございました、私はただそれをそこに修正しました:) – Solus

+0

あなたは大歓迎です! –

-1

に2列に値をしたい、私の出力でどのようにループのために以下の行を変更することについて -

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

いいえ、彼は時間と同じ行に名前を表示する必要がある –

+0

文字列を作成しているので、すべての馬を次々に印刷している'\ t'を使って' finishers'メソッドを呼び出します。それはあなたが変更しなければならないかもしれないもう一つのコードです。 – Veenit

+1

この投稿の編集または削除を検討すると、質問には答えず、少しの努力をします –

0

同じ機能で馬の名前と時間を追加します。

追加のためにStringBufferを使用します。より効率的です。ちょうどあなたが実際にはあまり計算している

System.out.println(showHorseThatFinished); 
関連する問題