截至目前,我的高分榜仅按猜测次数排序。我希望具有相同猜测数的条目也按时间(以ms为单位)按降序排序。尝试搜索并发现了一些类似的问题,但无法找到解决方案。任何指针将不胜感激!请随意评论我的代码的其他部分!
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);
}
}
}
}
答案 0 :(得分: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));
}
}
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
- 每当您需要根据超过1个属性进行排序时,请转到java.util.Comparator
界面。
- 使用compare()
的{{1}}方法和Comparator<T>
对您进行排序。