用户定义的String数组大小/输入Java

时间:2013-03-09 05:44:34

标签: arrays string input

我正在尝试用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"));

}

1 个答案:

答案 0 :(得分: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"));
  }
}

一:你的班级名为“主” - 这是临界的好,但应该大写。但是,我冒昧地将它重命名为与你的问题更相关的东西。

二:你应该在可能的情况下使用ArrayLists而不是“普通”数组 - 重新分配,释放内存和其他选项要比使用常规数组更好。

三:您应该使用开关 - 因为您的案例数超过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"));
  }
}