我正在编写一个程序来跟踪Round Robin锦标赛的结果。现在,我有一个无限循环,我认为我几天前修好了,但显然没有。我一直在跟踪setRanks方法,我认为这就是为什么它是无限循环的。我只需要弄清楚它为什么不能正确排序它们。
import java.util.ArrayList;
import java.util.Scanner;
public class RoundRobin
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many players?");
int num = Integer.parseInt(input.nextLine());
ArrayList<Player> entries = start(num);
roundScore(entries);
setRanks(entries);
System.out.println(entries);
}
public static ArrayList<Player> start(int num)
{
ArrayList<Player> entries = new ArrayList<Player>(num);
Player blank = new Player("");
for(int i = 0; i<num; i++)
{
entries.add(blank.copy());
}
for(int i = 0; i<entries.size(); i++)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter Player "+(i+1)+"'s name");
entries.get(i).editName(input.nextLine());
}
return entries;
}
public static ArrayList<Player> roundScore(ArrayList<Player> arr)
{
String[] strScore = new String[3];
int[] score = new int[3];
for(Player temp: arr)
{
Scanner input = new Scanner(System.in);
System.out.println("What is "+temp.getName()+"'s score?");
strScore = input.nextLine().split("-");
for(int i = 0; i<strScore.length; i++)
score[i] = Integer.parseInt(strScore[i]);
temp.setScores(score);
}
return arr;
}
public static ArrayList<Player> setRanks(ArrayList<Player> arr)
{
if(arr.size()<2)
return arr;
int index = 0;
int start = 1;
Player tempPlayer = new Player("");
int min;
while(!order(arr))
{
System.out.println(order(arr));
min = arr.get(start).getScore();
for(int i = 0; i<arr.size(); i++)
{
if(min<arr.get(i).getScore())
{
min=arr.get(i).getScore();
index = i;
}
tempPlayer = arr.get(index);
arr.set(index, arr.get(start-1));
arr.set(start-1, tempPlayer);
}
for(Player temp: arr)
temp.setRank(arr.indexOf(temp)+1);
}
return arr;
}//method end
private static boolean order(ArrayList<Player> arr)
{
for(int i = 0; i<arr.size(); i++)
{
for(int j = i+1; j<arr.size(); j++)
{
if(arr.get(j).getScore()>arr.get(i).getScore())
return false;
}
}
return true;
}//method end
}//class end
Player对象如下:
public class Player
{
private String name = "";
private int rank = 1;
public int roundWins = 0;
public int roundTies = 0;
public int roundLosses = 0;
public Player(String name)
{
this.name = name;
}//constructor end
public String toString()
{
return "\n "+name+"\n"+"W: "+roundWins+" T: "+roundTies+" L: "+roundLosses+"\n"+" Rank: "+rank;
}//method end
public void editName(String edit)
{
name = edit;
}//method end
public String getName()
{
return name;
}//method end
public void setRank(int place)
{
rank = place;
}//method end
public int getRank()
{
return rank;
}//method end
public void setScores(int[] arr)
{
if(arr[0]>arr[1])
roundWins++;
else if(arr[0]<arr[1])
roundLosses++;
else
roundTies++;
}//method end
public int getScore()
{
return roundWins-roundLosses;
}//method end
public Player copy()
{
Player copy = new Player(name);
copy.rank = rank;
copy.roundWins = roundWins;
copy.roundLosses = roundLosses;
copy.roundTies = roundTies;
return copy;
}//method end
public boolean equals(Player other)
{
if(this.name == other.name)
return true;
return false;
}//method end
}//class end
答案 0 :(得分:0)
将setRanks方法更改为:
public static ArrayList<Player> setRanks(ArrayList<Player> arr) {
if (arr.size() < 2)
return arr;
int index = 0;
int start = 1;
Player tempPlayer = new Player("");
int min;
Collections.sort(arr);
return arr;
}
并在Player类中实现Comparable接口:
public class Player implements Comparable<Player> {
...
@Override
public int compareTo(Player o) {
return getScore() - o.getScore();
}
}