Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按时间和猜测次数对高分进行排序_Java - Fatal编程技术网

Java 按时间和猜测次数对高分进行排序

Java 按时间和猜测次数对高分进行排序,java,Java,到目前为止,我的高分列表只按猜测的次数排序。我希望相同猜测次数的条目也能以毫秒为单位按时间降序排序。尝试搜索并找到了一些类似的Q,但无法真正找到解决方案。任何指点都将不胜感激!请随意评论我代码的其他部分 import java.util.*; public class Game { private static ArrayList<highScore> score = new ArrayList<highScore>(); private class highSco

到目前为止,我的高分列表只按猜测的次数排序。我希望相同猜测次数的条目也能以毫秒为单位按时间降序排序。尝试搜索并找到了一些类似的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);
            }
        }
     }
 }
import java.util.*;
公开课游戏{
私有静态ArrayList分数=新建ArrayList();
私有类highScore实现了可比性{
int猜测计数=0;
双播放时间=0;
弦乐演奏者姓名;
公开高分(整数猜测计数、双播放时间、字符串播放时间){
this.guessCount=猜测计数;
this.playerTime=playerTime;
this.playerName=playerName;
}
公共字符串toString(){
字符串scoreList=(this.playerName+“\t\t”+this.guestcount+“\t\t”+this.playerTime);
返回记分表;
}
公共int比较(高分hs){
如果(((整数)this.guestCount).compareTo(((整数)hs.guestCount))>0)
返回1;
其他的
返回-1;
}
}
公共静态void main(字符串[]args){
布尔playGame=true;
扫描仪扫描=新扫描仪(System.in);
游戏g=新游戏();
while(游戏){
g、 start();
System.out.println(“\n是否再次显示?”);
字符串s=scan.nextLine();
如果(s.equalsIgnoreCase(“n”)){
系统输出打印(“退出…”);
游戏=假;
}
}
}
公开作废开始(){
整数=(int)(Math.random()*1001);
int guess=-1;
int猜测计数=0;
字符串猜测,playerName;
String quit=“退出”;
扫描仪扫描=新扫描仪(System.in);
布尔播放=真;
System.out.print(“欢迎来到有史以来最伟大的猜谜游戏!”+
“\n请输入一个介于1-1000之间的数字!”+
“\n键入“退出”退出。”);
long startTime=System.currentTimeMillis();
System.out.println(“\n错误,编号为:“+编号”);
while(猜测!=数字和播放){
试一试{
System.out.print(“\n输入您的猜测:”);
guessStr=scan.nextLine();
if(猜测等信号情况(“退出”)){
玩=假;
System.out.println(“退出…”);
返回;
}
guess=Integer.parseInt(guessStr);
如果(猜测1000){
System.out.println(“无效猜测,不计算,再试一次!”);
}
如果(猜测<数字){
System.out.println(“太低,再试一次!”);
猜数++;
}
如果(猜测>数字){
System.out.println(“太高了,再试一次!”);
猜数++;
}
}
捕获(数字格式){
System.out.println(“抱歉,仅限数字。请重试!”);
}
捕获(输入不匹配异常异常){
System.out.println(“抱歉,仅限数字。请重试!”);
}
}
如果(猜测==数字){
long-endTime=System.currentTimeMillis();
长游戏时间=结束时间-开始时间;
System.out.println(“很好,正确的数字是“+number+”!”);
System.out.print(“你在“+(int)(游戏时间)/1000+”秒和“+guessCount+”次尝试之后就搞定了它!”);
System.out.println(“\n输入您的名字!”);
playerName=scan.nextLine();
添加(新的高分(猜数、游戏时间、玩家名称));
集合。排序(分数);
System.out.println(“名称-----猜测-----时间,毫秒”);
对于(高分h:分数){
系统输出打印ln(h);
}
}
}
}

<代码> > p>你只需要修改你的代码> Cuto to 方法,也可以考虑相等的情况。然后转到下一个比较:-

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

-每当您需要基于多个属性进行排序时,请转到
java.util.Comparator
接口


-使用
Comparator
compare()
方法以及
集合。sort(List l,Comparator c)
对列表进行排序。

compareTo
非常糟糕,您只需执行
返回Integer.valueOf(this.guestcount)。compareTo(hs.guestcount)
@AmitD。好的,谢谢。我今天才拿到250美元的赏金。但是答案有什么问题吗?您可以只在if
Integer.valueOf(this.playerTime).compareTo(hs.playerTime)
和在else
Double.valueOf(this.guestcount).compareTo(hs.guestcount)中使用
compareTo还应支持相等大小写,因此这将由另一种方式处理,即您的函数永远不会返回
0
,这意味着如果假设写的是相等,那么在这些条件下,两个对象相等,那么它们的compareTo永远不会返回
0
,即使它们是相等的equal@AmitD. 不,playerTime是双重类型的。这就是为什么我给出了一个
BigDecimal