因此,我重新學習了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: ");
}
}
}
這是我到目前爲止的代碼。如果任何人都可以給我一些建設性的批評和一些關於如何循環用戶輸入(測試輸入)並將輸入附加到數組的指示,我將不勝感激。
乾杯
如果你不介意,你能給我一個如何使用它的例子嗎? – hijaked79
請參閱編輯。還有一件事要添加到它:重複條目檢測。添加。 – fge
謝謝了。感謝評論代碼,我現在更好地理解它。 – hijaked79