0

我想为联盟中 20 支足球队的女孩随机分配球衣号码。

修复:我最终为每个团队使用 Collections.shuffle(shirtnums) 并处理顶部的数字。

这是我用来执行此操作的代码-

  • 我有一个整数和一个 ArrayList:

    int shirtnum = 0;
    ArrayList<Integer> shirtnums;
    shirtnums = new ArrayList<Integer>();
    
  • 在每个团队的开头,我将数字添加到其中:

    for(int number = 1; number < 31; number++){shirtnums.add(number);}
    
  • 在每个团队结束时,我都会清除它:

    shirtnums.clear();
    
  • 在 22 个女孩的循环中,我试图获取一个随机数,然后将其从 ArrayList 中删除:

    double shirt = Math.floor(Math.random() * shirtnums.size());
    shirtnum = (int) shirt;
    shirtnums.remove(shirtnum);
    

但是,这是一个团队的示例输出:

pid lname       fname       DOB         posid   tid shirtnum
1   Smith       Mary        1997-09-05  1       1   23
2   Johnson     Patricia    1997-04-03  2       1   15
3   Williams    Linda       1998-03-03  3       1   10
4   Jones       Barbara     1998-08-14  4       1   13
5   Brown       Elizabeth   1998-02-24  5       1   13
6   Davis       Jennifer    1998-08-27  6       1   12
7   Miller      Maria       1997-09-06  7       1   2
8   Wilson      Susan       1998-03-02  8       1   16
9   Moore       Margaret    1997-01-04  9       1   2
10  Taylor      Dorothy     1997-04-01  10      1   11
11  Anderson    Lisa        1997-01-25  11      1   7
12  Thomas      Nancy       1997-08-05  1       1   1
13  Jackson     Karen       1997-01-18  2       1   8
14  White       Betty       1997-01-16  3       1   5
15  Harris      Helen       1998-06-12  4       1   7
16  Martin      Sandra      1998-07-08  5       1   13
17  Thompson    Donna       1997-10-11  6       1   11
18  Garcia      Carol       1998-03-06  7       1   7
19  Martinez    Ruth        1998-08-00  8       1   7
20  Robinson    Sharon      1998-02-20  9       1   8
21  Clark       Michelle    1997-00-00  10      1   2
22  Rodriguez   Laura       1998-06-12  11      1   3

显然,我们不能让三个#13 和两个#2 在球场上跑来跑去……那我做错了什么?

根据要求,这是完整的代码:

import java.sql.*;
import java.lang.*;
import java.util.*;

public class GetSoccerNames
{
public static void main(String [] args)
{

    //declare variables
    Connection getconn  = null;
    Connection setconn  = null; 
    Statement getstmt       = null;
    ResultSet names_rs  = null;
    String lastname     = null;
    String firstname        = null;
    double year             = 0;
    double month            = 0;
    double day              = 0;
    String DOB              = null;
    int posid               = 0;
    int tid                     = 1;
    int shirtnum            = 0;
    int pid                 = 0;
    ArrayList<Integer> shirtnums;
    shirtnums = new ArrayList<Integer>();



    try{
        Class.forName("com.mysql.jdbc.Driver");
        getconn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/playernames", "root", "");

        //setconn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/league", "root", "");

        getstmt = getconn.createStatement();
        names_rs = getstmt.executeQuery("select fname, lname from names");

        //for each team
        for(tid = 1; tid < 4; tid++){           
            System.out.println("pid" + "\t" + "lname" + "\t" + "fname" + "\t" + "DOB" + "\t\t" + "posid" + "\t" + "tid" + "\t" + "shirtnum"); 

            //int[] shirtnums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};

           for(int number = 1; number < 31; number++){shirtnums.add(number);}

            //for each player on the team
            for(int teamplayer = 1; teamplayer < 23; teamplayer++){
                //PID
                // print i value ... remove for insert
                pid++;

                //NAME
                names_rs.next();
                lastname = names_rs.getString(2);
                firstname = names_rs.getString(1);

                //DOB
                year        = Math.round(Math.random() + (2013-16));
                month   = Math.round(Math.random() * 12);
                day         = Math.round(Math.random() * 28);
                int yyyy = (int) year;
                int mm  = (int) month;
                int dd  = (int) day;


                        if (mm>9 && dd>9)   {DOB = yyyy + "-" + mm + "-" + dd;}
                else    if (mm<10 && dd>9){DOB = yyyy + "-0" + mm + "-" + dd;}
                else    if (mm>9 && dd<10){DOB = yyyy + "-" + mm + "-0" + dd;}
                else    if (mm<10 && dd<10){DOB = yyyy + "-0" + mm + "-0" + dd;}

                //POSID
                posid = ((pid-1)%11)+1;

                //TID
                //tid is determined outside the loop

                //SHIRTNUM
                double shirt = Math.ceil(Math.random() * shirtnums.size());
                shirtnum = (int) shirt;
                shirtnums.remove(shirtnum - 1);


                //TEST PRINT
                System.out.println(pid + "\t" + lastname + "\t" + firstname + "\t" + DOB + "\t" + posid + "\t" + tid + "\t" + shirtnum); 
            }
            shirtnums.clear();
        }
    }
    catch(ClassNotFoundException cnfe){
        System.out.println("Class Not Found: " + cnfe.getMessage());        
    }
    catch(SQLException sqle){
        System.out.println("SQL Error: " + sqle.getMessage());
    }
    finally{
        if (getconn != null)        try{getconn.close();}       catch(SQLException ignore){}
        if (getstmt != null)        try{getstmt.close();}       catch(SQLException ignore){}
        if (names_rs != null)       try{names_rs.close();}          catch(SQLException ignore){}
    }
}
}

我希望这会有所帮助。

4

4 回答 4

2

更好的方法是使用Collections.shuffle()

这样:

Collections.shuffle(shirtnums, new Random(System.nanoTime()));

然后你可以简单地遍历洗牌列表:

for(Integer shirtnum : shirtnums) {
    ... etc
}

您可以为每个团队执行此操作。

于 2013-04-19T01:05:45.880 回答
1

为什么不使用Collections.shuffle(List<?>) 随机播放所有 33 个数字并从顶部发牌?

private void assignJerseys(int availableJerseys, List<Player> players) {

    assert players.size() < availableJerseys : "Not enough jerseys.";
    List<Integer> numbers = new ArrayList<>();
    for(int i = 1; i <= jerseys; ++i {
       numbers.add(i);
    }
    Collections.shuffle(numbers);
    for (int p = 0; p < players.size(); ++p) {
        players.get(p).setJerseyNumber(numbers.get(p));
    }
}
于 2013-04-19T01:10:30.300 回答
0

您的问题是您直接使用随机数作为衬衫编号,而不是数组内容。

您应该从数组的内容中检索您的衬衫号码,而不是直接从随机数中检索。

shirtnum = shirtnums.remove((int) shirt);
于 2013-04-19T00:59:34.010 回答
0

在将球衣号码分配给玩家之前,您不会检查是否已经从列表中删除了球衣号码。检查生成的随机衬衫号码是否可以解决您的问题。

于 2013-04-19T01:05:43.850 回答