Java 与Math.random()和ArrayList.remove()相关的逻辑

Java 与Math.random()和ArrayList.remove()相关的逻辑,java,math,random,arraylist,Java,Math,Random,Arraylist,我想给联盟中20支足球队的女孩随机分配球衣号码 修正:我最终为每个团队使用Collections.shuffleshirtnums,并在顶部处理数字 这是我用来做这件事的代码- 我有一个整数和一个数组列表: int shirtnum = 0; ArrayList<Integer> shirtnums; shirtnums = new ArrayList<Integer>(); double shirt = Math.floor(Math.random() * shirt

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

修正:我最终为每个团队使用Collections.shuffleshirtnums,并在顶部处理数字

这是我用来做这件事的代码-

我有一个整数和一个数组列表:

int shirtnum = 0;
ArrayList<Integer> shirtnums;
shirtnums = new ArrayList<Integer>();
double shirt = Math.floor(Math.random() * shirtnums.size());
shirtnum = (int) shirt;
shirtnums.remove(shirtnum);
在22个女孩的循环过程中,我尝试获取一个随机数,然后将其从ArrayList中删除:

int shirtnum = 0;
ArrayList<Integer> shirtnums;
shirtnums = new ArrayList<Integer>();
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){}
    }
}
}

我希望这会有所帮助。

您的问题是,您直接将随机数用作衬衫号码,而不是数组内容

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

shirtnum = shirtnums.remove((int) shirt);

您的问题是,您将随机数直接用于衬衫号码,而不是数组内容

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

shirtnum = shirtnums.remove((int) shirt);

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

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

更好的方法是使用

这样:

Collections.shuffle(shirtnums, new Random(System.nanoTime()));
然后,您可以简单地遍历无序列表:

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

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

更好的方法是使用

这样:

Collections.shuffle(shirtnums, new Random(System.nanoTime()));
然后,您可以简单地遍历无序列表:

for(Integer shirtnum : shirtnums) {
    ... etc
}
你可以为每个团队做这个。

为什么不使用集合。shuffleList将所有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));
    }
}
为什么不使用Collections.shuffleList将所有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));
    }
}

请共享完整代码问题似乎是,您正在为每个女孩的循环重置衬衫编号,这意味着您丢失了以前使用的衬衫编号的信息。请共享完整代码问题似乎是,你正在为每个女孩的衣领重置衬衫编号,这意味着你丢失了以前使用的衬衫编号的信息,所以我应该删除衬衫编号-1?这会抛出一个数组索引边界异常:-1Hmm,你能帮我发布你的整个衣领吗?我做了。谢谢你的关注和帮助。所以我应该删除shirtnum-1?这是在抛出一个ArrayIndexOutofBound异常:-1Hmm,你能帮我发布你的整个循环吗?我做了。谢谢你的关注和帮助。这就是我最终使用的。谢谢你的帮助!很遗憾,你不能洗牌堆栈,因为它不能实现列表。这就是我最后使用的。谢谢你的帮助!很遗憾,您不能洗牌堆栈,因为这并没有实现列表。