Java从用户创建字符串数组

时间:2014-07-09 02:45:29

标签: java arrays string

我正在试图弄清楚如何为这个梦幻足球计划添加新功能。我希望能够让用户为他们的团队命名,而不仅仅是“团队1”,“团队2”等。我正在尝试创建一个字符串数组和

  1. 向用户询问团队名称
  2. 在下一个问题中列出团队名称,询问周内的分数
  3. 在计划结束时列出最终结果中的团队名称
  4. 经过大量的搜索和阅读后,我无法弄清楚如何做到这一点。我目前要求用户提供for循环中的团队名称,我要求每周得分,这似乎不是正确的位置。任何帮助都会受到赞赏。

    import java.util.Scanner;
    
    public class fantasyFootball
    {
    
      private int numberOfTeams; // Same as teamAverage.length.
      private int numberOfWeeks; // Same as weekAverage.length.
    
      private int[][] score; //numberOfTeams rows and numberOfWeeks columns.
      private int[] teamAverage;
      private int[] scoreAverage;
    
      public static void main(String[] args)
      {
        fantasyFootball book = new fantasyFootball( );
        book.display();
      }
    
      public fantasyFootball(int[][] a)
      {
        if (a.length == 0 || a[0].length == 0)
        {
            System.out.println("Empty score records. Aborting.");
            System.exit(0);
        }
    
        numberOfTeams = a.length;
        numberOfWeeks = a[0].length;
        fillScore(a);
        fillTeamAverage( );
        fillScoreAverage( );
      }
    
      public fantasyFootball(fantasyFootball book)
      {
        numberOfTeams = book.numberOfTeams;
        numberOfWeeks = book.numberOfWeeks;
        fillScore(book.score);
        fillTeamAverage( );
        fillScoreAverage( );
      }
    
      public fantasyFootball( )
      {
        Scanner keyboard = new Scanner(System.in);
    
        System.out.println("Enter number of teams:");
        numberOfTeams = keyboard.nextInt( );
    
        System.out.println("Enter number of weeks:");
        numberOfWeeks = keyboard.nextInt( );
    
        score = new int[numberOfTeams][numberOfWeeks];
    
        for (int teamName = 1; 
                teamName <= numberOfTeams; teamName++)
            for (int weekNumber = 1; 
                    weekNumber <= numberOfWeeks; weekNumber++)
            {
                System.out.println("Enter team name ");
                teamName.add(keyboard.nextLine()); 
                System.out.println("Enter score for team "
                        + teamName);
                System.out.println("on week number " + weekNumber);
                score[teamName - 1][weekNumber - 1] =
                        keyboard.nextInt( );
            }
    
        fillTeamAverage( );
        fillScoreAverage( );
      }
    
      private void fillScore(int[][] a)
      {
        score = new int[numberOfTeams][numberOfWeeks];
    
        for (int teamName = 1; 
                teamName <= numberOfTeams; teamName++)
        {
            for (int weekNumber = 1; 
                    weekNumber <= numberOfWeeks; weekNumber++)
                score[teamName][weekNumber] = 
                a[teamName][weekNumber];
        }
      }
    
    /**
      Fills the array teamAverage using the data from the array score.
     */
      private void fillTeamAverage( )
      {
        teamAverage = new int[numberOfTeams];
    
        for (int teamName = 1; 
                teamName <= numberOfTeams; teamName++)
        {//Process one teamName:
            int sum = 0;
            for (int weekNumber = 1; 
                    weekNumber <= numberOfWeeks; weekNumber++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            //sum contains the sum of the week scores for team number teamName.
            teamAverage[teamName - 1] = sum/numberOfWeeks;
            //Average for team teamNumber is teamAverage[teamNumber - 1]
        }
      }
    
    /**
      Fills the array scoreAverage using the data from the array score.
     */
      private void fillScoreAverage( )
      {
        scoreAverage = new int[numberOfWeeks];
    
        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
        {//Process one week (for all teams):
            int sum = 0;
            for (int teamName = 1; 
                    teamName <= numberOfTeams; teamName++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            //sum contains the sum of all team scores on week number weekNumber.
            scoreAverage[weekNumber - 1] = sum/numberOfTeams;
            //Average for week weekNumber is the value of weekAverage[weekNumber - 1]
        }
      }
    
      public void display( )
      {
        for (int teamName = 1; 
                teamName <= numberOfTeams; teamName++)
    
        {//Display for one teamNumber:
            System.out.print("Team " + teamName + " Weeks: ");
            for (int weekNumber = 1; 
                    weekNumber <= numberOfWeeks; weekNumber++)
                System.out.print(score[teamName - 1][weekNumber - 1] + " ");
            System.out.println(" Ave = " + teamAverage[teamName - 1] );
        }
    
        System.out.println("Week averages: ");
        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
            System.out.print("Week " + weekNumber 
                    + " Ave = " + scoreAverage[weekNumber - 1] + " ");
        System.out.println( );
    }
    
      private void teamName()
      {
        Scanner keyboard = new Scanner(System.in);
        String[] teamName = new String[3];
    
      }
    

    }

4 个答案:

答案 0 :(得分:1)

以下是使用ArrayList存储团队名称的解决方案:

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

public class fantasyFootball {

    private List<String> teamNames = new ArrayList<String>();
    private int numberOfTeams; // Same as teamAverage.length.
    private int numberOfWeeks; // Same as weekAverage.length.

    private int[][] score; // numberOfTeams rows and numberOfWeeks columns.
    private int[] teamAverage;
    private int[] scoreAverage;

    public static void main(String[] args) {
        fantasyFootball book = new fantasyFootball();
        book.display();
    }

    public fantasyFootball(int[][] a) {
        if (a.length == 0 || a[0].length == 0) {
            System.out.println("Empty score records. Aborting.");
            System.exit(0);
        }

        numberOfTeams = a.length;
        numberOfWeeks = a[0].length;
        fillScore(a);
        fillTeamAverage();
        fillScoreAverage();
    }

    public fantasyFootball(fantasyFootball book) {
        numberOfTeams = book.numberOfTeams;
        numberOfWeeks = book.numberOfWeeks;
        fillScore(book.score);
        fillTeamAverage();
        fillScoreAverage();
    }

    public fantasyFootball() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter number of teams:");
        numberOfTeams = keyboard.nextInt();

        System.out.println("Enter number of weeks:");
        numberOfWeeks = keyboard.nextInt();

        score = new int[numberOfTeams][numberOfWeeks];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++){
            System.out.println("Enter team " + teamName + " name ");
            String name = keyboard.next();
            teamNames.add(name);
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++) {
                System.out.println("Enter score for team " + teamName + " on week number " + weekNumber);
                score[teamName - 1][weekNumber - 1] = keyboard.nextInt();
            }
        }

        fillTeamAverage();
        fillScoreAverage();
    }

    private void fillScore(int[][] a) {
        score = new int[numberOfTeams][numberOfWeeks];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++) {
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                score[teamName][weekNumber] = a[teamName][weekNumber];
        }
    }

    /**
     * Fills the array teamAverage using the data from the array score.
     */
    private void fillTeamAverage() {
        teamAverage = new int[numberOfTeams];

        for (int teamName = 1; teamName <= numberOfTeams; teamName++) {// Process
                                                                        // one
                                                                        // teamName:
            int sum = 0;
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            // sum contains the sum of the week scores for team number teamName.
            teamAverage[teamName - 1] = sum / numberOfWeeks;
            // Average for team teamNumber is teamAverage[teamNumber - 1]
        }
    }

    /**
     * Fills the array scoreAverage using the data from the array score.
     */
    private void fillScoreAverage() {
        scoreAverage = new int[numberOfWeeks];

        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++) {// Process
                                                                                // one
                                                                                // week
                                                                                // (for
                                                                                // all
                                                                                // teams):
            int sum = 0;
            for (int teamName = 1; teamName <= numberOfTeams; teamName++)
                sum = sum + score[teamName - 1][weekNumber - 1];
            // sum contains the sum of all team scores on week number
            // weekNumber.
            scoreAverage[weekNumber - 1] = sum / numberOfTeams;
            // Average for week weekNumber is the value of
            // weekAverage[weekNumber - 1]
        }
    }

    public void display() {
        for (int teamName = 1; teamName <= numberOfTeams; teamName++)

        {// Display for one teamNumber:
            System.out.print("Team " + teamNames.get(teamName-1) + " Weeks: ");
            for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
                System.out.print(score[teamName - 1][weekNumber - 1] + " ");
            System.out.println(" Ave = " + teamAverage[teamName - 1]);
        }

        System.out.println("Week averages: ");
        for (int weekNumber = 1; weekNumber <= numberOfWeeks; weekNumber++)
            System.out.print("Week " + weekNumber + " Ave = "
                    + scoreAverage[weekNumber - 1] + " ");
        System.out.println();
    }

    private void teamName() {
        Scanner keyboard = new Scanner(System.in);
        String[] teamName = new String[3];

    }
}

输出:

Enter number of teams:
3
Enter number of weeks:
2
Enter team 1 name 
MyTeam1
Enter score for team 1 on week number 1
1
Enter score for team 1 on week number 2
2
Enter team 2 name 
MyTeam2
Enter score for team 2 on week number 1
3
Enter score for team 2 on week number 2
4
Enter team 3 name 
MyTeam3
Enter score for team 3 on week number 1
4
Enter score for team 3 on week number 2
6
Team MyTeam1 Weeks: 1 2  Ave = 1
Team MyTeam2 Weeks: 3 4  Ave = 3
Team MyTeam3 Weeks: 4 6  Ave = 5
Week averages: 
Week 1 Ave = 2 Week 2 Ave = 4 

答案 1 :(得分:0)

我会在获得分数之前设置团队名称,

System.out.println("Enter number of teams:");
int numberOfTeams = keyboard.nextInt( );

String[] teamNames = new String[numberOfTeams];
for (int i = 0; i < numberOfTeams; i++) {
  System.out.printf("Enter team name for team %d:%n", 1+i);
  teamNames[i] = keyboard.next();
}

答案 2 :(得分:0)

        System.out.println("Enter team name ");
        teamName.add(keyboard.nextLine()); 
        System.out.println("Enter score for team "
                + teamName);

更改为:

        System.out.println("Enter team name ");
        String name = (keyboard.nextLine()); 
        System.out.println("Enter score for team "
                + name);

另外,如果你想存储它:

private String[] names = new String[numberOfTeams];
//Then in the above code:
names[teamName] = name;

答案 3 :(得分:0)

这是一个使用简单的String对象数组的解决方案。将private String[] names;添加到实例字段列表中,使FantasyFootball构造函数看起来像这样:

public FantasyFootball()
{
    int numTeams;
    int numWeeks;
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter number of teams: ");
    numTeams = keyboard.nextInt();

    for (int i = 0; i < numTeams; i++)
    {
        System.out.print("Enter the name of team " + (i+1) + ": ");
        names[i] = keyboard.nextLine();
    }

    System.out.print("Enter number of weeks: ");
    numWeeks = keyboard.nextInt();

    names = new String[numTeams];
    scores = new int[numTeams][numWeeks];

    for (int i = 0; i < numTeams; i++)
    {
        for (int j = 0; j < numWeeks; j++)
        {
            System.out.print("Enter the score for " + names[i] + " for week " + (j+1) + ": ");
            scores[i][j] = keyboard.nextInt();
        }
    }

    fillTeamAverage();
    fillScoreAverage();
}

一些编程提示:

首先,我建议创建一个Team对象,其中包含每周得分的数组,团队平均值的int和得分平均值的int。然后在你的fantasyFootball类中,只需操作一个Team对象数组,而不是每个不同字段的数组。它使将来更容易使用。

类名通常按惯例以大写字母开头。几乎每个人都遵循惯例,所以我强烈建议你也这样做。

使用one-liner-so-I-not-need-braces语法虽然较短,但是它使代码更难读取并且更难调试代码,所以我建议你使用所有的括号,以及在新行上而不是在语句行的末尾放置开括号。同样,它将使事情变得更加容易。

最后,在Java中,您可以使用+运算符将变量连接到字符串上,如您所知。但我在你的代码中注意到你打破了一些打印到两个调用。您可能已经知道这一点,但是您可以在同一个字符串/ print语句中使用多个+运算符,就像使用+和数字一样。

希望这有帮助!