我怀疑在列表中 4 teams
之间实施抽奖
我在下面有这个例子,并且它仅适用于结果不正确,代码有效,您可以点击链接http://ideone.com/bs9oAZ并点击此处
String[] teams = new String[4];
teams[0] = "BOSTON";
teams[1] = "CLEVELAND";
teams[2] = "SPURS";
teams[3] = "WARRIORS";
System.out.println("Number of teams:" + teams.length);
System.out.print("\n[ ");
for (String team : teams) {
System.out.print(team + " ");
}
System.out.println("]");
for (int i = 0; i < teams.length - 1; i++) {
System.out.format("\n--- Match %02d---\n", i + 1);
for (int j = 0; j < teams.length; j++) {
for (int k = j + 1; k < teams.length - 1; k++) {
System.out.println(teams[j] + " - " + teams[k]);
}
}
}
结果是第一轮
Number of teams: 4
[ BOSTON CLEVELAND SPURS WARRIORS ]
--- Match 01---
BOSTON - CLEVELAND
BOSTON - SPURS
CLEVELAND - SPURS
--- Match 02---
BOSTON - CLEVELAND
BOSTON - SPURS
CLEVELAND - SPURS
--- Match 03---
BOSTON - CLEVELAND
BOSTON - SPURS
CLEVELAND - SPURS
以及 4 teams
的正确结果,应该是第一轮
--- Match 01 ---
BOSTON - CLEVELAND
SPURS - WARRIORS
--- Match 02 ---
BOSTON - SPURS
CLEVELAND - WARRIORS
--- Match 03 ---
BOSTON - WARRIORS
CLEVELAND - SPURS
如果我能解决第一轮,我将尝试第二轮。
任何建议?
答案 0 :(得分:1)
你当然可以使用数组,但我确信使用ArrayLists会更容易。这是因为你正在将波士顿与&#34; X&#34;然后配对剩下的两个,其中ArrayList类有很多方便的方法。这里是您的代码(请注意,您仍然以字符串数组开头):
String[] teams = {"BOSTON", "CLEVELAND", "SPURS", "WARRIORS"};
System.out.println("Number of teams:" + teams.length);
System.out.print("\n[ ");
for (String team : teams)
System.out.print(team + " ");
System.out.println("]");
ArrayList<String> pair1, pair2;
for (int i = 1; i < teams.length ; i++)
{
pair1 = new ArrayList<String>(); // initializes this ArrayList
for(int p = 0; p < teams.length ; p++) // adds the contents of "teams"
pair1.add(teams[p]);
pair2 = new ArrayList<String>();
pair2.add(pair1.remove(i)); // pair1 has 4 elements,
pair2.add(pair1.remove(0)); // 2 of which are snatched and put in pair 2.
pair2.add(0,pair2.remove(1)); //swaps the elements in pair2
System.out.format("\n--- Match %02d---\n", i);
System.out.println(pair2.get(0)+"-"+pair2.get(1));
System.out.println(pair1.get(0)+"-"+pair1.get(1));
}
正如您所看到的,这个过程非常复杂,甚至可能不是最好的过程。但到目前为止,这是我提出的最简单的问题。如果你想要一个使用数组,我可以编辑它并将其放入。
答案 1 :(得分:1)
这就是我要做的事情。仅适用于偶数团队。如果您不理解它的任何部分,请随时问我。
public class Match {
public static int factorial(int num) {
int i = 1 ;
while(num != 1) {
i = i * num ;
num-- ;
}
return i ;
}
public static int search(int[] array) {
int var ;
for(var = 0 ; var < array.length ; var++ ) {
if(array[var] == 0) {
return var ;
}
}
return -1 ;
}
public static void main(String [] args) {
String[] teams = new String[4];
teams[0] = "BOSTON";
teams[1] = "CLEVELAND";
teams[2] = "SPURS";
teams[3] = "WARRIORS";
int [] opp = new int[4] ; // array that determines if a team has got an opposition team
int comb = (factorial(teams.length)) / (2 * factorial(teams.length - 2)) ; //find no.of possible combinations
int match = comb / (teams.length/2) ; // find number of matches
for(int i = 0 ; i < opp.length ; i++) { //clear array
opp[i] = 0 ;
}
System.out.println("Number of teams:" + teams.length);
System.out.print("[ ");
for (String team : teams) {
System.out.print(team + " ");
}
System.out.print("]\n");
for (int j = 1 ; j <= match ; j++) {
System.out.format("\n--- Match %02d---\n", j);
System.out.println(teams[0] + " - " + teams[j]); //set the first team and its opponent
opp[0] = 1 ; // first team has been set
opp[j] = 1 ; // opponent has been set
for(int i = 2 ; i <= teams.length/2 ; i++) { //first pair has been set, start from second pair
int var1 = search(opp); // look for next team without opponent
opp[var1] = 1 ;
int var2 = search(opp); // look for next team without opponent
opp[var2] = 1 ;
System.out.println(teams[var1] + " - " + teams[var2]);
}
for(int i = 0 ; i < opp.length ; i++) { //clear the array
opp[i] = 0 ;
}
}
}
}