2013-03-11 105 views
-3

我正在寫一個程序,允許用戶根據他們進入球隊進入運動隊,然後記錄輸贏。我非常確定我的陣列設置是錯誤的,因爲它沒有正確地增加輸贏,有沒有人看到這個問題?遞增陣列值Java

import java.util.Scanner; 

public class sports { 


public static void main(String[] args) { 


    System.out.println("Howdy sports fan!"); 

    String menuSelect; 
    String winSelect; 
    String loseSelect; 
    int teamSize = 0; 

    String[] teamsArray = new String[0]; 
    int[] winsArray = new int[0]; 
    int[] lossesArray = new int[0]; 


    do { 

     System.out.println("Please pick an option from the list below:"); 
     System.out.println("1) Create League"); 
     System.out.println("2) List all teams"); 
     System.out.println("3) Record a win");   
     System.out.println("4) Record a loss");   
     System.out.println("5) Quit");   
     Scanner keyboard = new Scanner(System.in); 
     menuSelect = keyboard.nextLine(); 

     if (menuSelect.equals("1")) 
     { 

      System.out.println("How many teams should I make?"); 
      try 
      { 
      teamSize = Integer.parseInt(keyboard.nextLine()); 
      } 
      catch (NumberFormatException e) 
      { 
       System.out.println("Invalid entry, try again."); 
      } 

      teamsArray = new String[teamSize]; 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.println("Team " + (i+1) + "'s name?"); 
       teamsArray[i] = keyboard.nextLine();     
      } 
     } 

     else if (menuSelect.equals("2")) 
     { 

      if (teamsArray.length == 0) 
      { 
       System.out.println("There are no teams!"); 
      } 
      else 


      System.out.printf("%21s %21s %n", "W", "L"); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.printf(teamsArray[i] + "%20d %21d %n", winsArray[i], lossesArray[i]); 

      } 
     } 

     else if (menuSelect.equals("3")) 
     { 
      winsArray = new int[teamSize]; 
      System.out.println("Which team won a game?"); 
      winSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (winSelect.equals(teamsArray[i])) 
       { 
        ++winsArray[i]; 
       } 
      } 
     } 

     else if (menuSelect.equals("4")) 
     { 
      lossesArray = new int[teamSize]; 
      System.out.println("Which team lost a game?"); 
      loseSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (loseSelect.equals(teamsArray[i])) 
       { 
        ++lossesArray[i]; 
       } 
      } 
     } 
     else 
     { 
      System.out.println("Invalid Selection"); 
     } 


    } while(!menuSelect.equals("5")); 

} 

} 
+2

什麼是輸入,什麼是預期結果,什麼是實際結果?你用過調試器嗎? – 2013-03-11 05:23:43

回答

1

你必須在一個時間,而不是在選項#3/4的這樣的初始化初始化在選項#1中的所有陣列。

import java.util.Scanner; 

public class sports { 


public static void main(String[] args) { 


    System.out.println("Howdy sports fan!"); 

    String menuSelect; 
    String winSelect; 
    String loseSelect; 
    int teamSize = 0; 

    String[] teamsArray = new String[0]; 
    int[] winsArray = new int[0]; 
    int[] lossesArray = new int[0]; 


    do { 

     System.out.println("Please pick an option from the list below:"); 
     System.out.println("1) Create League"); 
     System.out.println("2) List all teams"); 
     System.out.println("3) Record a win");   
     System.out.println("4) Record a loss");   
     System.out.println("5) Quit");   
     Scanner keyboard = new Scanner(System.in); 
     menuSelect = keyboard.nextLine(); 

     if (menuSelect.equals("1")) 
     { 

      System.out.println("How many teams should I make?"); 
      try 
      { 
      teamSize = Integer.parseInt(keyboard.nextLine()); 
      } 
      catch (NumberFormatException e) 
      { 
       System.out.println("Invalid entry, try again."); 
      } 

      teamsArray = new String[teamSize]; 
      winsArray = new int[teamSize]; 
      lossesArray = new int[teamSize]; 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.println("Team " + (i+1) + "'s name?"); 
       teamsArray[i] = keyboard.nextLine();     
      } 
     } 

     else if (menuSelect.equals("2")) 
     { 

      if (teamsArray.length == 0) 
      { 
       System.out.println("There are no teams!"); 
      } 
      else 


      System.out.printf("%21s %21s %n", "W", "L"); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       System.out.printf(teamsArray[i] + "%20d %21d %n", winsArray[i], lossesArray[i]); 

      } 
     } 

     else if (menuSelect.equals("3")) 
     { 
      System.out.println("Which team won a game?"); 
      winSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (winSelect.equals(teamsArray[i])) 
       { 
        ++winsArray[i]; 
       } 
      } 
     } 

     else if (menuSelect.equals("4")) 
     { 
      System.out.println("Which team lost a game?"); 
      loseSelect = keyboard.nextLine(); 

      for (int i = 0; i < teamsArray.length; ++i) 
      { 
       if (loseSelect.equals(teamsArray[i])) 
       { 
        ++lossesArray[i]; 
       } 
      } 
     } 
     else 
     { 
      System.out.println("Invalid Selection"); 
     } 


    } while(!menuSelect.equals("5")); 

} 

} 
+0

不要幫人寫一個完整的code.Tell他自己的錯誤,讓他改正。 如果給一個完整的代碼是這樣的人不會知道什麼將是他的錯誤。 – 2013-03-11 05:59:23

0

的問題是當你閱讀贏或輸,你是分配winsArraylossesArray。下面的兩個語句應該是選項1:

winsArray = new int[teamSize]; 
lossesArray = new int[teamSize]; 
0

如果您intializing陣列這樣,編譯器thinkes這三個變量teamsArray, winsArray和lossesArray與零號數組類型。這意味着您可以在數組中添加任何值,並且當您嘗試像teamsArray [i]一樣訪問時會導致錯誤。

String[] teamsArray = new String[0]; 
int[] winsArray = new int[0]; 
int[] lossesArray = new int[0]; 

所以嘗試初始化數組,當用戶選擇一個創建一個聯盟作爲ravidra說。

teamsArray = new String[teamSize]; 
winsArray = new int[teamSize]; 
lossesArray = new int[teamSize]; 

嘗試瞭解出了什麼問題以及如何糾正它。

+0

我在這裏學習,有些事情可能會造成混淆,但我看到了什麼差錯在這種情況下。感謝您的幫助 – user2150807 2013-03-11 06:10:47