2016-12-07 68 views
-2

該程序應該存儲2個團隊名稱和2個分數的輸入,在這樣做之後它應該輸出它們。輸出「null」的Java數組

數組只是印刷 「NULL [空] |空[空]」

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 - 這些值爲空,因爲您沒有正確設置它們。在你的課程中可能爲時過早,但我建議創建一個Java對象來封裝這些值,並使用適當的toString方法來打印它們。你可以有一個更容易處理的對象列表。 – duffymo

+0

不應該在'for循環'結尾的'System.out.println'語句超出for循環的範圍嗎?首先你需要在打印出來之前設置所有的值。我把'System.out.println'語句放在for循環之後,我嘗試編譯你的代碼,並且它工作正常。 – Yousaf

回答

0

你只是在做一個簡單的錯誤。要保存價值,你的String []與數組索引我應像反轉

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

      } 

數組索引從0開始,並要保存你不同的指數

的價值,你應該試試這個!

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

您應該學習如何調試!