1

我正在尝试用 Java 编写一个菜单驱动的程序。不过,我在使用 for 循环读取用户输入到我的字符串数组时遇到了一些麻烦。当我将数组从 String 更改为 int 时,代码工作正常。但是,当我将其更改为字符串时,它会在用户有机会输入团队名称之前通过循环两次。我还需要让用户根据他们想要输入的团队数量来控制数组的大小,所以如果他们想输入 5 个团队,那么数组的大小将是 5。如果我在用户输入之前声明数组数组大小然后它不起作用。而且我不能只将其保留在 if 语句中,否则会出现范围问题。任何人都可以看到解决这个问题的方法吗?这是程序的第一部分

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

}
4

1 回答 1

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

一:你有你的班级命名为“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"));
  }
}
于 2013-03-09T06:31:52.210 回答