0

我需要帮助对我尝试过 Holder-comarator、Collections.Sort 的输入文件/高分进行排序,而我现在正试图将其设为数组列表,因此高分按我尝试过但失败的时间排序,我真的被卡住了好几天。任何建议都会很可爱

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

public class game {

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";
    String Gissning;

    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 || (!( input.next().equals(quit)))){   


        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"))){
           game.start();
       }
       else 
           System.out.print("Thanks for playing");
       System.exit(0);

}


    public static void main (String[] args) throws IOException {

       game.start();
    }
}
4

1 回答 1

0

有很多方法可以实现这一目标。可能最简单的方法是在将分数打印到屏幕之前对其进行排序。首先,将分数线放在一个列表中。

BufferedReader infile = new BufferedReader(new FileReader(scorefile));
List<String> scores = new ArrayList<String>();
while ((line = infile.readLine()) != null) {
    scores.add(line);
}
infile.close();

现在您可以对列表进行排序。您可以使用该类的sort方法Collections,但您必须为此使用自定义Comparator方法。由于您的高分格式与空格处的行一样<name>\t<steps>\t<time>split因此将第三个元素解析为数字并比较这些数字。

Collections.sort(scores, new Comparator<String>() {
    public int compare(String s1, String s2) {
        int t1 = Integer.parseInt(s1.split("\\s")[2]); // time in s1
        int t2 = Integer.parseInt(s2.split("\\s")[2]); // time in s2
        return t1 - t2;                                // neg. if t1 < t2
    } 
});

这将对scores就地列表进行排序,所以现在剩下要做的就是实际打印分数:

System.out.println("Name       Steps  Time");
for (String s : scores) {
    String[] ps = s.split("\\s");
    System.out.println(String.format("%-10s %5s %5s", ps[0], ps[1], ps[2]));
}
于 2012-09-19T21:28:57.987 回答