2012-12-23 66 views
0

因此,我重新學習了java,這已經有一段時間了。我正在嘗試構建一個基本程序(在代碼註釋中進行了解釋),我無法記住如何接受用戶輸入並將其添加到數組中。我在記憶如何遍歷用戶輸入和測試它們是否輸入任何內容以及如果輸入內容將輸入追加到數組中時遇到了更多的麻煩。接受用戶輸入並將其添加到數組

//This program will ask user for for there favorite four games 
//If the answer is blank, it will ask again for a game title 
//The program will than store there answers into an array 
//The program will than display the array in random order 
//it will then give the amount of games in the array with an integer 



import java.util.*; 

public class MultipleClassesMain { 


public static void main(String[] args) { 

    //Array holds 4 string inputs from user 
    String gameArray[] = new String[4]; 

    //importing scanner element------- 
    Scanner input = new Scanner(System.in); 

    //Introduction--------------- 
    System.out.println("Hey there!!!"); 
    System.out.println("Please tell us four game titles you like to play!!!"); 

    //Asks what game user likes and takes user input into a variable 
    System.out.println("So what a game you like?: "); 
    String temp = input.nextLine(); 

    //This loop will test against blank user input 
    while (temp.equals("") || (temp.equals(" ")){ 
     System.out.println("Your game can't be blank. Enter again: "); 

     } 

    } 

}

這是我到目前爲止的代碼。如果任何人都可以給我一些建設性的批評和一些關於如何循環用戶輸入(測試輸入)並將輸入附加到數組的指示,我將不勝感激。

乾杯

回答

4

第一:使用List代替用戶輸入陣列。只需.add()您的投入。但請參閱下面的更好的解決方案,即使用Set

其次:String有一個.trim()方法,它可以在開始和結束時刪除空格,使用它並使用.isEmpty()來測試空字符串。

第三種:List不檢測重複條目,然而Set確實,只要它的條目正確執行equals()hashCode(),這String呢,所以下面的代碼佔了這一點(的Set返回true,如果.add()方法並且僅在該操作的結果被修改時)。

示例代碼:

public static void main(final String... args) 
{ 
    // Set of valid user inputs 
    final Set<String> gameList = new HashSet<String>(); 
    // Object from which user inputs are read 
    final Scanner in = new Scanner(System.in); 

    // Introduction 
    System.out.println("Hey there!!"); 
    System.out.println("Please tell us four game titles you like to play!!!"); 

    // What the user enters 
    String input; 

    // Check that 4 titles have been entered, don't get out of the loop until then 
    while (gameList.size() < 4) { 
     System.out.print("Enter the name of a game: "); 
     // Read one input, trim all beginning and trailing whitespaces 
     input = in.nextLine().trim(); 
     // If the resulting string is empty, input is invalid: ask for another 
     if (input.isEmpty()) { 
      System.out.println("Empty inputs not accepted!"); 
      continue; 
     } 
     if (!gameList.add(input)) 
      System.out.println("You have already selected this game (" + input + ')'); 
    } 

    // Print out the list of inputs 
    System.out.println("The list of selected games is: " + gameList); 

} 
+0

如果你不介意,你能給我一個如何使用它的例子嗎? – hijaked79

+1

請參閱編輯。還有一件事要添加到它:重複條目檢測。添加。 – fge

+0

謝謝了。感謝評論代碼,我現在更好地理解它。 – hijaked79

2
for (int i = 0; i < 4; i++) { 
     String temp = input.nextLine(); 
     if (temp.equals("") || (temp.equals(" "))) { 
      System.out.println("Your game can't be blank. Enter again: "); 
      i--; 
     } else 
      gameArray[i] = temp; 

    } 

試試這個。這是你要求的。

+0

我相信如此。我忘了我必須迭代列表/數組以確保每個點都填充了輸入。 – hijaked79

相關問題