我想给一个联盟中20支足球队的女生分配随机的球衣号码。
FIXED:我最终为每支球队使用了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){}
}
}
}
我希望有所帮助。
答案 0 :(得分:2)
更好的方法是使用Collections.shuffle()。
这样:
Collections.shuffle(shirtnums, new Random(System.nanoTime()));
然后你可以简单地遍历洗牌清单:
for(Integer shirtnum : shirtnums) {
... etc
}
您可以为每个团队执行此操作。
答案 1 :(得分:1)
为什么不使用Collections.shuffle(List<?>)
随机播放所有三十三个号码并将其排除在顶部?
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));
}
}
答案 2 :(得分:0)
您的问题是您是直接使用衬衫编号的随机数,而不是数组内容。
您应该从数组的内容中检索您的衬衫编号,而不是直接从随机编号中检索。
shirtnum = shirtnums.remove((int) shirt);
答案 3 :(得分:0)
在将球衣号码分配给播放器之前,您不会检查衬衫号码是否已从列表中删除。检查生成的随机衬衫号码是否可以解决您的问题。