简体   繁体   中英

What's causing my infinite loop?

I'm writing a program to track the results of a Round Robin tournament. Right now, I have an infinite loop that I thought I fixed a few days ago, but apparently not. I've been tracing the setRanks method and I think that's why it's infinite-looping. I just need a hand figuring out why it won't order them correctly.

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

The Player object is as follows:

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

Change setRanks method to:

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;
}

and implement Comparable interface in Player class:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM