您的代码存在许多问题,我尝试按照以下方法进行更正:
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"));
}
}
一:你有你的班级命名为“main”——这是正常的,但应该大写。但是,我冒昧地将其重命名为与您的问题更相关的名称。
二:您应该尽可能使用 ArrayLists 而不是“普通”数组- 因为您重新分配、取消分配内存和其他选项比使用常规数组要好得多。
三:您应该使用 switch - 因为您的案例数量超过 4(这是编写菜单代码以提高可读性的一般经验法则)。
除了这些,我认为这应该非常适合您的问题。
在您的情况下,自从您执行了keyboard.nextInt()
. 尽管您正确读取了整数,但尚未读取换行符。因此,当keyboard.nextLine()
被调用时,它会读取换行符- 这给您的印象是您已经循环了“两次”并且没有拾取您的第二个输出(实际上它有,但您不知道,或者看到)。这也是为什么当您将它作为字符串使用时,它会捕获换行符并且捕获工作完美无缺。
更新:
编辑为使用静态数组与 ArrayLists:
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"));
}
}