2013-03-09 131 views
1

我正試圖用Java編寫一個菜單驅動的程序。儘管使用for循環來讀取我的字符串數組的用戶輸入,但我遇到了一些麻煩。當我將數組從String更改爲int時,代碼工作正常。但是,當我將其更改爲字符串時,它會在用戶有機會輸入團隊名稱之前經過兩次循環。我還需要讓用戶根據他們想要輸入的隊伍數量來控制陣列的大小,所以如果他們想輸入5個隊伍,那麼陣列的大小就是5.如果我在用戶輸入之前聲明數組數組大小,那麼它不起作用。我不能把它放在if語句中,否則會產生範圍問題。任何人都可以看到一種解決方法嗎?下面是該計劃的第一位用戶自定義字符串數組大小/輸入Java

import java.util.Scanner; 

public class main { 


public static void main(String[] args) { 

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

    String menuSelect; 

    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?"); 
      String[] teamsArray= new String[keyboard.nextInt()]; 

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

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

} 

回答

1

代碼有一些與它的問題,我試圖糾正按如下:

import java.util.Scanner; 
import java.util.*; 

public class SportsLeague { 

    public static void main(String[] args) { 

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

    String menuSelect; 
    Scanner keyboard = new Scanner(System.in); 
    List<String> teamsArray = new ArrayList<String>(); 

    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");   

     menuSelect = keyboard.nextLine(); 

     //since you're making a menu, switches are better 
     //this is especially so since your program exceeds 4 options 
     //which is a generic rule of thumb for readability 
     switch(menuSelect) { 
     case "1": 
      System.out.println("How many teams should I make?"); 

      //reset the teamsArray, so that you can create a new league 
      //you may want to change this if you want 
      teamsArray.clear(); 

      //get the number of teams with which to create names for 
      //by converting the line from the keyboard to an integer 
      int teams = Integer.parseInt(keyboard.nextLine()); 

      //now iterate over it and assign values to the array by 
      //prompting the user for the info and saving it using 
      //the add() method 
      for (int i = 0; i < teams; ++i) 
      { 
      System.out.println("Team " + (i+1) + "'s name?"); 
      teamsArray.add(keyboard.nextLine());  
      } 
      break;//break is required here 

     //print out the contents of the teamsArray 
     case "2": 
      for (int i = 0; i < teamsArray.size(); ++i) 
      { 
      //print out the elements within the arraylist using the "get()" method 
      System.out.println(teamsArray.get(i)); 
      } 
      break; 

     //implement for the other options... 
     } 
    } while(!menuSelect.equals("5")); 
    } 
} 

一:你有你的類名爲「主」 - 這是邊界沒關係,但應該大寫。但是,我冒昧地將它重命名爲與您的問題更相關的內容。

二:你應該使用的ArrayList,而不是「正常」的陣列,其中可能 - 當你重新分配,釋放內存和其他選項好得多比你將與一個普通陣列做。

三:您應該使用開關 - 因爲您的案件數量超過了4個(這是編寫菜單代碼以提高可讀性的一般規則)。

除了那些,我認爲這應該很適合你的問題。

就你而言,自從你做了keyboard.nextInt()以來,循環被讀取兩次。 雖然,您正確讀取整數,但換行符並未被讀取。因此,當keyboard.nextLine()被調用時,它讀取換行符 - 這給你的印象是,你已經「兩次」循環,並沒有拿起你的第二個輸出(實際上它有,但你不知道,或參見)。 這也是爲什麼當你將它作爲一個字符串時,它捕捉到了換行符,並且捕獲工作完美無缺。

UPDATE:

編輯使用靜態數組對的ArrayList:

import java.util.Scanner; 
import java.util.*; 

public class SportsFan3 { 

    public static void main(String[] args) { 

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

    String menuSelect; 
    Scanner keyboard = new Scanner(System.in); 

    String[] teamsArray = new String[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");   

     menuSelect = keyboard.nextLine(); 

     switch(menuSelect) { 
     case "1": 

      //set the number of teams within array to 0 

      //check to see that the number of teams that the user has enetered does not exceed the maximumTeamsize 

      int numteams = 0; 

      System.out.println("How many teams should I make?"); 
      numteams = Integer.parseInt(keyboard.nextLine()); 

      teamsArray = new String[numteams]; 

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

     case "2": 
      for (int i = 0; i < teamsArray.length; ++i) 
      { 
      System.out.println(teamsArray[i]); 
      } 
      break; 

     //implement for the other options... 
     } 
    } while(!menuSelect.equals("5")); 
    } 
} 
+0

謝謝你的回覆jrd1。我對ArrayLists並不是很熟悉,而且我們班上還沒有談到他們。我可以使用標準數組來完成這項任務嗎?或者是更多迂迴方法 – user2150807 2013-03-09 06:42:37

+0

@ user2150807,您可以使用標準數組來完成此任務,但是您會遇到內存限制的問題。讓我詳細說明一下:這一切都與Java中的事實有關,就像在C中一樣,一旦創建數組,它就不能改變其大小。但是,您可以通過在內存中創建一個非常大的數組並相對於索引交換/分配/重新分配值來規避此內存大小。然而,這種情況下用戶可以請求創建比陣列大小更多的團隊 - 這會導致問題。 – jrd1 2013-03-09 06:54:46

+0

這意味着超過陣列大小的隊伍數量將會丟失,永遠不會被存儲 - 這是你不想發生的事情(根本)。當然,你總是可以通過確保團隊數量不超過數組大小來進行綁定檢查 - 這非常好。但是,使用普通數組時非常重要的另一件事是 - 它可能效率非常低 - 特別是對於大尺寸。 – jrd1 2013-03-09 06:57:01