Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting - Fatal编程技术网

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

Java 排序高分。排序时间输入的困难,java,sorting,Java,Sorting,我对输入排序有困难,我希望它先按最短时间排序。我是java新手,所以我不知道我做了多少数字游戏,但我无法按最低时间排序高分,这就是我到目前为止所做的 import java.io.*; import java.util.*; public class teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeest { private static void start() throws IOException { int number = (int) (Mat

我对输入排序有困难,我希望它先按最短时间排序。我是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);

    }
}   
import java.io.*;
导入java.util.*;
公共类TEEEEEEEEST{
私有静态void start()引发IOException{
整数=(int)(Math.random()*1001);
缓冲读取器;
reader=新的BufferedReader(新的InputStreamReader(System.in));
扫描仪输入=新扫描仪(System.in);
String scorefile=“p-lista_java”;
int försök=0;
int-gissning=0;
字符串namn;
字符串行=null;
弦y;
字符串n;
字符串val;
String quit=“退出”;
System.out.println(“您好,欢迎来到这个猜谜游戏”+
“\n开始猜测它是一个介于1和1000之间的数字:”;
long startTime=System.currentTimeMillis();
虽然(正确){
System.out.print(“\n输入您的猜测:”);
gissning=input.nextInt();
försök++;
如果(gissning==数字){
long-endTime=System.currentTimeMillis();
长游戏时间=结束时间-开始时间;
System.out.println(“是的,数字是”+number+
“\n您在“+(int)(游戏时间/1000)+”秒“+”中的“+försök+”猜测“+”次后得到它。”;
System.out.print(“请输入您的姓名:”);
namn=reader.readLine();
试一试{
缓冲写入器输出文件
=新的BufferedWriter(新的FileWriter(scorefile,true));
outfile.write(namn+“”+försök+“\t”+(int)(gameTime/1000)+“\n”);
outfile.close();
}捕获(IOException异常){
}
打破
}
如果(gissning<1 | | gissning>1000){
System.out.println(“愚蠢的猜测!我不会数那个…”);
--försök;
}
否则如果(gissning>编号)
System.out.println(“您的猜测太高了”);
其他的
System.out.println(“您的猜测太低”);
}
试一试{
缓冲读写器填充
=新的BufferedReader(新的文件读取器(scorefile));
而((line=infle.readLine())!=null){
系统输出打印项次(行);
}
infle.close();
}捕获(IOException异常){
}
System.out.println(“是否要继续(Y/N)”;
val=reader.readLine();
如果((val.equals(“y”))|(val.equals(“y”)){
teeeeeeeest.start();
}
其他的
System.out.print(“感谢您的参与”);
系统出口(0);
}
}   

创建一个值对象,该对象保存您的所有得分、时间和其他详细信息。创建一个比较器,比较两个支架之间的组件。排序可以通过创建一组带有比较器的保持器来实现

如果您希望按其他属性以不同的顺序进行排序,只需根据需要更新比较器

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;
   }
}
支架{
长时间;
智力得分;
字符串名;
}
比较器{
int比较(支架支架,支架其他){
int结果=holder.time-other.time;
如果(0==结果){
结果=holder.score-其他.score;
}
返回结果;
}
}

我不太清楚你想要什么。Java有很好的排序功能,你为什么不先读一本关于它的教程呢?