2016-12-07 23 views
-2

プログラムは2つのチーム名と2つのスコアの入力を保存した後、出力する必要があります。配列がちょうど印刷され"null"を出力するJava配列

"ヌル[ヌル] |ヌル[ヌル]"

import java.util.Scanner; 


public class C3484209 { 

    public static void main(String[] args) { 

     String[] home_team_name = new String[100]; 
     String[] away_team_name = new String[100]; 
     String[] home_team_score = new String[100]; 
     String[] away_team_score = new String[100]; 

     Scanner keyboard = new Scanner(System.in); 

     String line = ""; 

     while (!(line.equalsIgnoreCase("stop"))) 
     { 

       System.out.println("Enter the teams that played and the score :"); 
       System.out.println("(Please note that the standard format is Home team : Away Team : Home Score : Away Score)"); 
       line = keyboard.nextLine(); 

       String[] elements = line.split(": "); 


        for (int i = 0; i < elements.length;) 
        { 
         if ((int)i == 0) 
         { 
          home_team_name[i] = elements[0]; 
          i++; 
         } 
         else if ((int)i == 1) 
         { 
          away_team_name[i] = elements[1]; 
          i++; 
         } 
         else if ((int)i == 2) 
         { 
          home_team_score[i] = elements[2]; 
          i++; 
         } 
         else if((int)i == 3) 
         { 
          away_team_score[i] = elements[3]; 
          i++; 
         } 
         //System.out.print("Element " + (i+1) + " was : " + elements[i] + " | "); 
         System.out.println(home_team_name[i] + " [" + home_team_score[i] + "]" + " | " + away_team_name[i] +" [" + away_team_score[i] + "]"); 

        } 

     } 
     //Output Command 

     keyboard.close(); 

    } 

} 
+3

これはプログラミングのクラスにイントロで簡単な宿題のように見えます。あなたの最良の賭けは、あなたの前提が現実に合致しないところを見るためのデバッガでのスピンです。 JVMを信じてください。あなたが正しく設定していないので、これらの値はnullです。あなたのコースでは早すぎるかもしれませんが、これらの値をカプセル化するJavaオブジェクトを作成し、それを印刷する適切なtoStringメソッドを使用することをお勧めします。あなたは、扱いがはるかに簡単なオブジェクトのリストを持つことができます。 – duffymo

+0

あなたの 'forループ'の終わりにある 'System.out.println'文は、そのforループの範囲外にあるべきではありませんか?まず、すべての値を印刷する前に設定する必要があります。 'forループ'から 'System.out.println'文を出してコードをコンパイルしようとしましたが、うまくいきました。 – Yousaf

答えて

0

あなたは単純な間違いをしています。あなたは[]文字列あなたに、配列は、インデックス0から始まり、あなたはあなたがこれを試してみてください別のインデックスで

に価値を保存している

if ((int) i == 0) { 
       home_team_name[k] = elements[i]; 

      } 

のように逆転されなければならないインデックスiを持つ配列を値を保存しています!

package stackoverflow; 
import java.util.Scanner; 
public class C3484209 { 
public static void main(String[] args) { 

    String[] home_team_name = new String[100]; 
    String[] away_team_name = new String[100]; 
    String[] home_team_score = new String[100]; 
    String[] away_team_score = new String[100]; 

    Scanner keyboard = new Scanner(System.in); 

    String line = ""; 

    int k = 0; 
    while (!(line.equalsIgnoreCase("stop"))) { 

     System.out.println("Enter the teams that played and the score :"); 
     System.out.println(
       "(Please note that the standard format is Home team : Away Team : Home Score : Away Score)"); 
     line = keyboard.nextLine(); 

     String[] elements = line.split(": "); 

     for (int i = 0; i < elements.length; i++) { 
      if ((int) i == 0) { 
       home_team_name[k] = elements[i]; 

      } else if ((int) i == 1) { 
       away_team_name[k] = elements[i]; 

      } else if ((int) i == 2) { 
       home_team_score[k] = elements[i]; 

      } else if ((int) i == 3) { 
       away_team_score[k] = elements[i]; 

      } 
     } 
     System.out.println(home_team_name[k] + " [" + home_team_score[k] + "]" + " | " + away_team_name[k] + " [" 
       + away_team_score[k] + "]"); 
     k++; 

    } 
    keyboard.close(); 
} 

}

0

これを試してみてください!

package cn.sehzh; 

import java.util.Scanner; 

public class Main { 
    public static void main(String[] args) { 

     String[] home_team_name = new String[100]; 
     String[] away_team_name = new String[100]; 
     String[] home_team_score = new String[100]; 
     String[] away_team_score = new String[100]; 

     Scanner keyboard = new Scanner(System.in); 

     String line = ""; 

     int j = 0; 
     while (!(line.equalsIgnoreCase("stop"))) { 

      System.out.println("Enter the teams that played and the score :"); 
      System.out.println(
        "(Please note that the standard format is Home team : Away Team : Home Score : Away Score)"); 
      line = keyboard.nextLine(); 

      String[] elements = line.split(": "); 

      for (int i = 0; i < elements.length;) { 
       if ((int) i == 0) { 
        home_team_name[j] = elements[0]; 
        i++; 
       } else if ((int) i == 1) { 
        away_team_name[j] = elements[1]; 
        i++; 
       } else if ((int) i == 2) { 
        home_team_score[j] = elements[2]; 
        i++; 
       } else if ((int) i == 3) { 
        away_team_score[j] = elements[3]; 
        i++; 
       } 
      } 
      // System.out.print("Element " + (i+1) + " was : " + elements[i] 
      // + " | "); 
      System.out.println(home_team_name[j] + " [" + home_team_score[j] + "]" + " | " + away_team_name[j] + " [" 
        + away_team_score[j] + "]"); 
      j++; 

     } 
     // Output Command 

     keyboard.close(); 
    } 
} 

デバッグ方法を学ぶ必要があります。