无法收集扫描仪数据

时间:2014-09-16 18:35:17

标签: java inheritance arraylist polymorphism

问题:如何收集用户输入的所有信息,将它们存储到ArrayList和
中  显示所有输入的答案?我应该输入六个队名,胜利,分区等,并将它们显示在一个带有System.out.println()的表中;正如我在for-loop结束时尝试的那样

public class PlayoffSelectorClass extends Team {

// main method
   public static void main(String[] args) {
       Team team1 = new Team();
       Team team2 = new Team();
       Team team3 = new Team();
       Team team4 = new Team();
       Team team5 = new Team();
       Team team6 = new Team();

       for (int i = 0; i < 6; i++) {

           Scanner input = new Scanner(System.in);

           System.out.println("Please enter team name: ");
           String name = input.nextLine();

           System.out.println("\nPlease enter the city " + name + " played in: ");
           String city = input.nextLine();

           System.out.println("\nPlease enter the division " + name + " play in: ");
           String division = input.nextLine();

           System.out.println("\nPlease enter the number of wins " + name + " has: ");
           Integer wins = input.nextInt();

           System.out.println("\nPlease enter the number of losses " + name + " has: ");
           Integer loses = input.nextInt();

           if (i < 5) {
               System.out.println("\nEnter your next team...\n");
           }

           team1.setTeamName(name);
           team1.setCity(city);
           team1.setDivision(division);
           team1.setWins(wins);
           team1.setLoses(loses);

       }

       System.out.println("East   W   L   PCT");

       System.out.println(team1.getTeamName() + " " + team1.getWins() + " " + team1.getLoses());
    System.out.println(team1.getTeamName() + " " + team1.getWins() + " " + team1.getLoses());


   }
}

3 个答案:

答案 0 :(得分:0)

如果您确定您想要的团队数量为6,则声明一个类似

的数组
List<Team> teams = new ArrayList<Team>();

这应该是一个设计变更,你不断保持Team1,Team2 ......等等。

当你想保存数据时

Team team = null;
for (int i = 0; i < 6; i++) {
team = new Team();
System.out.println("Please enter team name: ");
String teamName = input.nextLine();
team.setTeamName(teamName);
... // Similarly the rest. 
teams.add(team);

当您从控制台获取输入时,还会进行一些额外的检查。检查是否为空字符串或仅输入。这些检查将使代码更加无错误。

答案 1 :(得分:0)

如果你为每个玩家明确声明一个变量,你将很难将用户的输入存储到正确的变量中(除非你做了一些讨厌的事情,比如if(i==3) team3.setName(name))。最好创建一个数组列表(List<Team> teams = new ArrayList<Team>),并在每个循环中创建一个新的Team对象(Team team = new Team();),您要填充(team.setName(name)等)。在每次迭代结束时,此对象将添加到您的数组列表(teams.add(team))中。在所有六次迭代之后,您将拥有一个包含用户创建的每个团队的数组列表。现在,您可以通过以下方式遍历此列表:

for(Team team: teams) {
  //do stuff with the team object, e.g.:
  System.out.println(team.getName());
}

答案 2 :(得分:0)

您可以像这样使用List

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PlayoffSelector {

    static class Team {
        private String teamName;
        private String city;
        private String division;
        private Integer wins;
        private Integer loses;

        public void setTeamName(String teamName) {
            this.teamName = teamName;
        }

        public String getTeamName() {
            return teamName;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCity() {
            return city;
        }

        public void setDivision(String division) {
            this.division = division;
        }

        public String getDivision() {
            return division;
        }

        public void setWins(Integer wins) {
            this.wins = wins;
        }

        public Integer getWins() {
            return wins;
        }

        public void setLoses(Integer loses) {
            this.loses = loses;
        }

        public Integer getLoses() {
            return loses;
        }
    }

    public static void main(String[] args) {
        List<Team> teams  = new ArrayList<>();

        for (int i = 0; i < 2; i++) {

            Scanner input = new Scanner(System.in);

            System.out.println("Please enter team name: ");
            String name = input.nextLine();

            System.out.println("\nPlease enter the city " + name + " played in: ");
            String city = input.nextLine();

            System.out.println("\nPlease enter the division " + name + " play in: ");
            String division = input.nextLine();

            System.out.println("\nPlease enter the number of wins " + name + " has: ");
            Integer wins = input.nextInt();

            System.out.println("\nPlease enter the number of losses " + name + " has: ");
            Integer loses = input.nextInt();

            if (i < 5) {
                System.out.println("\nEnter your next team...\n");
            }

            Team team = new Team();
            team.setTeamName(name);
            team.setCity(city);
            team.setDivision(division);
            team.setWins(wins);
            team.setLoses(loses);

            teams.add(team);

        }

        System.out.println("East W   L   PCT");

        for (Team team : teams) {
            System.out.printf("%s\t%s\t%s\n",team.getTeamName(), team.getWins() , team.getLoses());
        }

    }
}