2016-03-01 185 views
-1

我正在編寫一個程序來跟蹤循環賽比賽的結果。現在,我有一個無限循環,我想我前幾天修復了,但顯然不是。我一直在追蹤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 

玩家對象如下:

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 
+1

'系統。 out.println'是你的朋友。 – codeCompiler77

+0

爲什麼你不使用可用的內置排序方法?沒有必要寫你自己的。 –

+1

Java也有調試器功能 –

回答

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類可比接口:

public class Player implements Comparable<Player> { 
... 

    @Override 
    public int compareTo(Player o) { 
     return getScore() - o.getScore(); 
    } 
} 
+0

有沒有辦法將它從最大分類到最小? –

+0

快速解決方案:使用o.getScore調用getScore() 'return o.getScore() - getScore();' – hya

相關問題