所以我正在重新学习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: ");
}
}
}
这是我到目前为止的代码。如果有人能给我一些建设性的批评和一些关于如何循环用户输入(测试输入)并将输入附加到数组的指示,我将不胜感激。
干杯