英文:
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 < count; i++);
System.out.println("Enter your time");
for (int i=1; i < names.length; i++);
System.out.println("Enter your name");
}
}
答案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<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));
}
}
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<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<>();
//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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论