1

截至目前,我的高分列表仅按猜测次数排序。我希望具有相同猜测次数的条目也按时间(以毫秒为单位)按降序排序。尝试搜索并找到了一些类似的 Q,但无法真正找到解决方案。任何指针将不胜感激!也可以随意评论我的代码的其他部分!

import java.util.*;

public class Game {

private static ArrayList<highScore> score = new ArrayList<highScore>();

private class highScore implements Comparable<highScore> {

    int guessCount = 0;
    double playerTime = 0;
    String playerName;

    public highScore (int guessCount, double playerTime, String playerName) {
        this.guessCount = guessCount;
        this.playerTime = playerTime;
        this.playerName = playerName;
    }

    public String toString() {
        String scoreList = (this.playerName + "\t\t" + this.guessCount + "\t\t" + this.playerTime);
        return scoreList;
    }

    public int compareTo(highScore hs) {
        if (((Integer)this.guessCount).compareTo(((Integer)hs.guessCount)) > 0)
            return 1;
        else
            return -1;
    }
}

public static void main(String [] args) {

    boolean playGame = true;
    Scanner scan = new Scanner(System.in);

    Game g = new Game();

    while(playGame) {
        g.start();

        System.out.println("\nPlay again?");
        String s = scan.nextLine();
            if (s.equalsIgnoreCase("n")) {
                System.out.print("Quitting...");
                playGame = false;
        }
    }
}

public void start() {

    int number = (int) (Math.random() * 1001 );
    int guess = -1;
    int guessCount = 0;
    String guessStr, playerName;
    String quit = "quit";
    Scanner scan = new Scanner(System.in);
    boolean play = true;

        System.out.print("Welcome to the greatest guessing game of all time!" + 
        "\nGuess a number between 1-1000!" + 
        "\nType \"quit\" to quit.");

        long startTime = System.currentTimeMillis();

        System.out.println("\nDEBUG, nr is: " + number);

        while (guess != number && play) {
            try {
                System.out.print("\nEnter your guess: ");
                guessStr = scan.nextLine();
                    if (guessStr.equalsIgnoreCase("quit")) {
                        play = false;
                        System.out.println("Quitting...");
                        return;
                }
                guess = Integer.parseInt(guessStr);

                if (guess <= 1 || guess > 1000) {
                    System.out.println("Invalid guess, won't count, try again!");
                }
                if (guess < number) {
                    System.out.println("Too low, try again!");
                    guessCount++;
                }
                if (guess > number) {
                    System.out.println("Too high, try again!");
                    guessCount++;
                }
            }
            catch(NumberFormatException e) {
                System.out.println("Sorry, only numbers. Try again!");
            }
            catch (InputMismatchException ex) {             
                System.out.println("Sorry, only numbers. Try again!");
            }
        }

        if (guess == number) {
            long endTime = System.currentTimeMillis();
            long gameTime = endTime - startTime;

            System.out.println("Nice, the correct number is " + number + "!");
            System.out.print("You nailed it after " + (int)(gameTime)/1000 + " seconds and " + guessCount + " tries!");
            System.out.println("\nEnter your name!");

            playerName = scan.nextLine();
            score.add(new highScore(guessCount, gameTime, playerName));
            Collections.sort(score);

            System.out.println("Name ------- Guesses -------- Time in ms");
            for (highScore h: score) {
                System.out.println(h);
            }
        }
     }
 }
4

3 回答 3

2

你只需要修改你的compareTo方法来考虑相等的情况。然后进行下一个比较:-

public int compareTo(highScore hs) {

    if (this.guessCount == hs.guessCount) {
         return (new BigDecimal(this.playerTime)).compareTo(
                 new BigDecimal(hs.playerTime)
    } else {
         return Integer.valueOf(this.guessCount).compareTo(
                Integer.valueOf(hs.guessCount));
    }
}
于 2012-10-23T11:54:05.417 回答
0

highScore 的 compareTo() 方法必须实现如下

public int compareTo(highScore hs)
{
    if (((Integer)this.guessCount).compareTo(((Integer)hs.guessCount)) > 0)
        return 1;
    else
    {
        if(this.guessCount == hs.guessCount)
        {
          if(this.playerTime > hs.playerTime)
          {
             return 1;
          }
          else
          {
             return -1;
          }
        }

        return -1;
    }
}
于 2012-10-23T12:00:50.787 回答
0

-每当您需要根据超过 1 个属性进行排序时,请选择java.util.Comparator接口。

-使用连同的compare()方法对您的列表进行排序。Comparator<T>Collections.sort(List l, Comparator c)

于 2012-10-23T12:04:58.133 回答