程序无法将用户输入分配给数组下标

时间:2017-03-19 01:24:29

标签: java arrays string input subscript

我的程序应该提示您输入一个团队名称,然后输入四个团队成员的名字,然后再循环三次。然后它应该显示每个团队名称及其相应的成员,例如: 断路器团队的成员: 沃尔特,杰西,斯凯拉,扫罗。 相反,它会将每个团队列为"团队",所以它会说"团队成员"对于他们四个人而言,我似乎无法找到它的错误。

String teamName;                           
   String[] members = new String[4];

   public String getTeamName()
   {
      return teamName;
   }
   public void setTeamName(String team)
   {
      teamName = "team";          
   }



   public void setMember(int number, String name)
   {
      members[number] = name;
   }

   public String getMember(int number)
   {
      return members[number];
   }

   public static void main(String[] args)
   {

   }

}






















public static void main(String[] args) 
       {
          String name;
          final int NUM_TEAMS = 4;
          BowlingTeam[] teams = new BowlingTeam[NUM_TEAMS];
          int x;
          int y;
          final int NUM_TEAM_MEMBERS = 4;
          getTeamData(teams);
          for(y = 0; y < NUM_TEAMS; ++y)
          {
             System.out.println("\nMembers of team " + teams[y].getTeamName());
             for(x = 0; x < NUM_TEAM_MEMBERS; ++x)      
                System.out.print(teams[y].getMember(x) + " "); 
             System.out.println();
          }
          name = JOptionPane.showInputDialog(null,"Enter a team name to see its roster.");
          for(y = 0; y < teams.length; ++y)
            if(name.equals(teams[y].getTeamName()))
               for(x = 0; x < NUM_TEAM_MEMBERS; ++x)
                  System.out.print(teams[y].getMember(x));
          System.out.println();
       }

       public static void getTeamData(BowlingTeam[] teams)
       {
          String name;
          final int NUM_TEAMS = 4;
          int x;
          int y;
          final int NUM_TEAM_MEMBERS = 4;
          for(y =  0; y  <  NUM_TEAMS; ++y)               
          {
             teams[y] = new BowlingTeam(); 
             name = JOptionPane.showInputDialog(null, "Enter team name");
             teams[y].setTeamName(name);
             for(x = 0; x < NUM_TEAM_MEMBERS;   ++x)
             {
               name = JOptionPane.showInputDialog(null, "Enter team member's name");
               teams[y].setMember(x, name);
             }
          }
       }
    }

1 个答案:

答案 0 :(得分:0)

设置团队名称存在错误:

    String teamName;
String[] members = new String[4];

public String getTeamName()
{
    return teamName;
}
public void setTeamName(String team)
{
    // Here you need to assign the team to teamName, not "team"
    teamName = team;
}



public void setMember(int number, String name)
{
    members[number] = name;
}

public String getMember(int number)
{
    return members[number];
}

public static void main(String[] args)
{

}

这对我有用:

public class BowlingTeam {

String teamName;
String[] members = new String[4];

public String getTeamName() {
    return teamName;
}

public void setTeamName(String team) {
    teamName = team;
}

public void setMember(int number, String name) {
    members[number] = name;
}

public String getMember(int number) {
    return members[number];
}


public static void main(String[] args) {
    String name;
    final int NUM_TEAMS = 4;
    BowlingTeam[] teams = new BowlingTeam[NUM_TEAMS];
    int x;
    int y;
    final int NUM_TEAM_MEMBERS = 4;
    getTeamData(teams);
    for (y = 0; y < NUM_TEAMS; ++y) {
        System.out.println("\nMembers of team " + teams[y].getTeamName());
        for (x = 0; x < NUM_TEAM_MEMBERS; ++x)
            System.out.print(teams[y].getMember(x) + " ");
        System.out.println();
    }
    name = JOptionPane.showInputDialog(null, "Enter a team name to see its roster.");
    for (y = 0; y < teams.length; ++y)
        if (name.equals(teams[y].getTeamName()))
            for (x = 0; x < NUM_TEAM_MEMBERS; ++x)
                System.out.print(teams[y].getMember(x));
    System.out.println();
}

public static void getTeamData(BowlingTeam[] teams) {
    String name;
    final int NUM_TEAMS = 4;
    int x;
    int y;
    final int NUM_TEAM_MEMBERS = 4;
    for (y = 0; y < NUM_TEAMS; ++y) {
        teams[y] = new BowlingTeam();
        name = JOptionPane.showInputDialog(null, "Enter team name");
        teams[y].setTeamName(name);
        for (x = 0; x < NUM_TEAM_MEMBERS; ++x) {
            name = JOptionPane.showInputDialog(null, "Enter team member's name");
            teams[y].setMember(x, name);
        }
    }
}

}