编写一个用于运行竞赛的数组。

huangapple go评论72阅读模式
英文:

Writing an Array for running competition

问题

import java.util.Arrays;
import java.util.Collections;

public class question2 {
    public static void main(String[] args) {
        final int count = 3;
        int temp;

        double[] time = new double[count];
        double sum = 0;
        String[] names = new String[3];

        java.util.Scanner input = new java.util.Scanner(System.in);
        for (int i = 0; i < count; i++) {
            System.out.println("Enter your time");
        }
        for (int i = 0; i < names.length; i++) {
            System.out.println("Enter your name");
        }
    }
}
英文:

> Write a program that asks for the names of three runners and the time, in minutes, it took each of them to finish a race. The program should display the names of the runners in the order that they finished.

I'm having a problem figuring out how to code this question, I'm facing issues arranging the times in ascending order then printing out the runner's names instead of their time.

import java.util.Arrays;
import java.util.Collections;

public class question2 {
	public static void main(String[] args) {
		final int count = 3;
		int temp;
		
		double[] time = new double[count];
		double sum = 0;
		String[] names = new String[3];
		
		java.util.Scanner input = new java.util.Scanner(System.in);
		for (int i=0; i &lt; count; i++);
			System.out.println(&quot;Enter your time&quot;);
		for (int i=1; i &lt; names.length; i++);
			System.out.println(&quot;Enter your name&quot;);
	}
}

答案1

得分: 1

你可以定义一个名为Player的类,包括姓名和时间属性。然后,通过实现接口Comparable使该类可以进行比较。接着,重写compareTo()toString()方法来进行比较和生成适当的输出。最后,创建一个可以排序的Player对象数组。

请查看以下代码:

import java.util.Arrays;
import java.util.Scanner;

class Player implements Comparable<Player> {
    public String name;
    public double time;

    public Player(String name, double time) {
        this.name = name;
        this.time = time;
    }

    @Override
    public String toString() {
        return String.format("%s: %.2f", name, time);
    }

    @Override
    public int compareTo(Player o) {
        return (int) (this.time - o.time);
    }
}

public class question2 {
    public static void main(String[] args) {
        final int count = 3;

        Player[] playerData = new Player[count];
        Scanner input = new Scanner(System.in);

        for (int i = 0; i < count; i++) {
            System.out.println("Enter name and time: ");
            playerData[i] = new Player(input.next().strip(), input.nextDouble());
        }

        Arrays.sort(playerData);
        System.out.println(Arrays.toString(playerData));
    }
}

在这里,我使用了Arrays.sort()方法,它接受一个数组,并使用Player类中实现的compareTo()方法进行排序。

英文:

You could define a Player class having name and time. Then also make the class comparable by implementing the interface Comparable. Then override the methods compareTo() and toString() for comparison and proper output. Then have an array of Player objects, which can be sorted.

Check out the code:

import java.util.Arrays;
import java.util.Scanner;

class Player implements Comparable&lt;Player&gt; {
    public String name;
    public double time;

    public Player(String name, double time) {
        this.name = name;
        this.time = time;
    }

    @Override
    public String toString() {
        return String.format(&quot;%s: %.2f&quot;, name, time);
    }

    @Override
    public int compareTo(Player o) {
        return (int) (this.time - o.time);
    }
}

public class question2 {
    public static void main(String[] args) {
        final int count = 3;

        Player[] playerData = new Player[count];
        Scanner input = new Scanner(System.in);

        for (int i = 0; i &lt; count; i++) {
            System.out.println(&quot;Enter name and time: &quot;);
            playerData[i] = new Player(input.next().strip(), input.nextDouble());
        }

        Arrays.sort(playerData);
        System.out.println(Arrays.toString(playerData));
    }
}

Here I use Arrays.sort() which takes in the array and sort using the implemented compareTo() method of the Player class.

答案2

得分: 0

以下是翻译好的代码部分:

import java.util.*;
class Runner{
    String name;
    int time;
    public Runner(String name, int time){
        this.name = name;
        this.time = time;
    }
}

class SortByTime implements Comparator<Runner>{
    public int compare(Runner r1, Runner r2){
        return r1.time - r2.time;
    }
}
public class MyClass {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        List<Runner> runners = new ArrayList<>();
        // 输入示例:
        /*
            姓名1
            时间1
            姓名2
            时间2
        */
        while(sc.hasNext()){
            runners.add(new Runner(sc.nextLine(), sc.nextInt()));
            sc.nextLine();
        }
        Collections.sort(runners, new SortByTime());
        for(Runner ro : runners){
            System.out.println(ro.name);
        }
    }
}
英文:

Here is the code

import java.util.*;
class Runner{
    String name;
    int time;
    public Runner(String name,int time){
        this.name=name;
        this.time=time;
    }
}

class SortByTime implements Comparator&lt;Runner&gt;{
    public int compare(Runner r1,Runner r2){
        return r1.time-r2.time;
    }
}
public class MyClass {
    public static void main(String args[]) {
      Scanner sc=new Scanner(System.in);
      List&lt;Runner&gt; runners=new ArrayList&lt;&gt;();
      //input be like
      /*
        name1
        t1
        name2
        t2
      */
      while(sc.hasNext()){
        runners.add(new Runner(sc.nextLine(),sc.nextInt()));
        sc.nextLine();
      }
      Collections.sort(runners,new SortByTime());
      for(Runner ro:runners){
          System.out.println(ro.name);
      }
    }
}

huangapple
  • 本文由 发表于 2020年9月20日 12:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63975406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定