排序高分。排序时间输入的困难

时间:2012-09-18 01:45:42

标签: java sorting

我正在对输入进行排序,我希望它首先按最低时间排序。我是java的新手所以我不知道这么多,我已经做了一个猜数字游戏,但我无法在最低时间排序高分,这是我到目前为止所做的。

import java.io.*;
import java.util.*;

public class teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeest {

    private static void  start() throws IOException {
        int number = (int) (Math.random() * 1001);
        BufferedReader reader;
        reader = new BufferedReader(new InputStreamReader(System.in));
        Scanner input = new Scanner(System.in);

        String scorefile = "p-lista_java";
        int försök = 0;
        int gissning = 0;
        String namn;
        String line = null;
        String y;
        String n;
        String val ;
        String quit = "quit";

        System.out.println("Hello and welcome to this guessing game" +
                "\nStart guessing it's a number between 1 and 1000:");

        long startTime = System.currentTimeMillis();
        while (true){   
            System.out.print("\nEnter your guess: ");
            gissning = input.nextInt();
            försök++;



            if (gissning == number ){
                long endTime = System.currentTimeMillis();
                long gameTime = endTime - startTime;
                System.out.println("Yes, the number is " + number + 
                        "\nYou got it after " + försök + " guesses " + " times in " + (int)(gameTime/1000) + " seconds.");
                System.out.print("Please enter your name: ");
                namn = reader.readLine();


                try {
                    BufferedWriter outfile
                            = new BufferedWriter(new FileWriter(scorefile, true));
                    outfile.write(namn + " " + försök +"\t" + (int)(gameTime/1000) + "\n");
                    outfile.close();
                } catch (IOException exception) {

                }


                break;

            }



             if( gissning < 1 || gissning > 1000 ){
                    System.out.println("Stupid guess! I wont count that..." );
                    --försök;
             }

             else if (gissning > number)
                System.out.println(" Your guess is too high");

            else
                System.out.println("Your guess is too low");
            }



            try {
                BufferedReader infile
                            = new BufferedReader(new FileReader(scorefile));
                while ((line = infile.readLine()) != null) {
                    System.out.println(line);
                }
                infile.close();
            } catch (IOException exception) {


        }

        System.out.println("Do you want to continue (Y/N)?");
           val=reader.readLine();

           if ((val.equals("y"))||(val.equals("Y"))){
               teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeest.start();
           }
           else 
               System.out.print("Thanks for playing");
           System.exit(0);

    }
}   

1 个答案:

答案 0 :(得分:2)

创建一个包含所有评分,时间和其他详细信息的值对象。创建一个比较器,用于比较其中两个持有者之间的组件。可以通过使用比较器创建一组持有者来实现排序。

如果您希望按其他顺序排序其他属性,只需根据需要更新比较器。

Holder {
    long time;
    int score;

    String name;
}

Comparator<Holder> {
    int compare( Holder holder, Holder other ){
       int result = holder.time - other.time;
       if( 0 == result ){
          result = holder.score - other.score;
       }
        return result;
   }
}