我创建了一个名为marathon的项目,我向用户询问名称,开始时间,开始时间,分钟,开始时间,结束时间,结束时间和结束时间。截至目前,它告诉名称开始和结束时间和持续时间小时:最小:我的问题是我不知道如何让比较器设置为持续时间并将它们变成排序列表(所以最低的跑步者)时间在顶部,最慢在底部。)
这是主要的
/* @author maxkidd
*
* Marathon takes name start time and end time and saves it in a collection also
* calculates duration of runners race
*/
package marathon;
import java.util.List;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.*;
@SuppressWarnings("unused")
public class Marathon {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner scanner = new Scanner( System.in );
// Create a List of Runners
int response = 0;
while(response != -1) {
List<Runner> list = new ArrayList<Runner>();
// Prompt the user to enter runners
// while loop here
//while (!response.equals("N"))
// After user enters each runner
// Create a runner object
try {
System.out.println("Enter Runner name");
String name= input.next();
System.out.print("Enter starting hour ");
int start_hours = input.nextInt();
System.out.print("Enter starting minute ");
int start_mins = input.nextInt();
System.out.print("Enter starting second ");
int start_seconds = input.nextInt();
System.out.print("Enter ending hour ");
int end_hours = input.nextInt();
System.out.print("Enter ending minute");
int end_mins = input.nextInt();
System.out.print("Enter ending second ");
int end_seconds = input.nextInt();
// Calculate Duration
int duration = ((end_hours*3600+end_mins*60+end_seconds)
-(start_hours*3600+start_mins*60+start_seconds))/3600;
int durationM = ((end_mins*60+end_seconds)
-(start_mins*60+start_seconds))/60;
int durationS= (end_seconds
-start_seconds);
int place = 1;
// int durationS = durationM/60;
Runner runner = new Runner(
place, name,
start_hours,
start_mins,
start_seconds,
end_hours,
end_mins,
end_seconds,
duration,
durationM,
durationS
);
// Add runner to the list
list.add(runner);
// Output the List of Runners
System.out.printf("NAME START:TIME END:TIME DURATION %n");
// Loop through the list of runners and print them out
for (Runner r : list) {
System.out.printf("%s ", r.getName());
r.displayStartTime();
r.displayEndTime();
r.displayduration();
System.out.printf(" %n");
}
System.out.print("If you would like to stop enter -1 or add another runner press 1 ");
response = input.nextInt();
System.out.printf(" Response is %d " , response);
} catch(InputMismatchException inputMismatchException) {
System.out.print("Enter a number next time please! ");
}
}
}
}
这是第二个为其提供r.displays
的类package marathon;
public class Runner {
private String name;
private int StartHours;
private int StartMins;
private int StartSeconds;
private int EndHours;
private int EndMins;
private int EndSeconds;
private int Duration;
private int DurationHours;
private int DurationMinutes;
private int durationSeconds;
private int place;
public Runner(
int place,
String name,
int StartHours,
int StartMins,
int StartSeconds,
int EndHours,
int EndMins,
int EndSeconds,
int DurationHours,
int DurationMinutes,
int durationSeconds) {
this.name = name;
this.StartHours = StartHours;
this.StartMins = StartMins;
this.StartSeconds = StartSeconds;
this.EndHours = EndHours;
this.EndMins = EndMins;
this.EndSeconds = EndSeconds;
this.DurationHours = DurationHours;
this.DurationMinutes = DurationMinutes;
this.durationSeconds = durationSeconds;
}
public void displayduration() {
System.out.printf("%d Hour(s):%d Minute(s):%d Second(s) ", DurationHours,DurationMinutes, durationSeconds);
}
public void displayStartTime() {
System.out.printf("%d:%d:%d ", StartHours,StartMins,StartSeconds);
}
public void displayEndTime() {
System.out.printf("%d:%d:%d ", EndHours, EndMins, EndSeconds);
}
public void setDuration(int Duration) {
this.Duration = Duration;
}
public int getDuration() {
return this.Duration;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPlace() {
return place;
}
public void setPlace(int place) {
this.place = place;
}
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
你需要像这样
使你的Runner
对象可比较
class Runner implements Comparable<Runner>{
....
@Override
public int compareTo(Author other){
// compareTo should return < 0 if this is supposed to be
// less than other, > 0 if this is supposed to be greater than
// other and 0 if they are supposed to be equal
if (this.hours > other.getHours())
return 1;
else if (this.hours < other.getHours())
return -1;
else {
if (this.minutes > other.getMinutes())
return 1;
else if (this.minutes > other.getMinutes())
return -1;
else {
if (this.seconds > other.getSeconds())
return 1;
else if (this.seconds > other.getSeconds())
return -1;
else {
return 0;
}
}
}
}
}
你可以在utils
中使用内置函数对任何Runner列表进行排序答案 1 :(得分:0)
我喜欢MadProgrammer的想法,并稍微改编了Runner类,所以它实际上使用了Duration。另外,我重新编写了Marathon类来使用TreeSet,它会根据跑步者的持续时间自动排序。
/* @author maxkidd
*
* Marathon takes name start time and end time and saves it in a collection also
* calculates duration of runners race
*/
import java.time.Duration;
import java.time.LocalTime;
import java.util.*;
@SuppressWarnings("unused")
public class Marathon {
@SuppressWarnings({"resource", "unused"})
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
// Create a List of Runners
int response = 0;
while (response != -1) {
Set<Runner> set = new TreeSet<>(Comparator.comparing(Runner::getDuration));
// Prompt the user to enter runners
// while loop here
//while (!response.equals("N"))
// After user enters each runner
// Create a runner object
try {
System.out.println("Enter Runner name");
String name = input.next();
System.out.print("Enter starting hour ");
int start_hours = input.nextInt();
System.out.print("Enter starting minute ");
int start_mins = input.nextInt();
System.out.print("Enter starting second ");
int start_seconds = input.nextInt();
System.out.print("Enter ending hour ");
int end_hours = input.nextInt();
System.out.print("Enter ending minute");
int end_mins = input.nextInt();
System.out.print("Enter ending second ");
int end_seconds = input.nextInt();
LocalTime startTime = LocalTime.of(start_hours, start_mins, start_seconds);
LocalTime endTime = LocalTime.of(end_hours, end_mins, end_seconds);
Duration duration = Duration.between(startTime, endTime);
int place = 1;
Runner runner = new Runner(
place,
name,
startTime,
endTime,
duration
);
set.add(runner);
// Output the Set of Runners
System.out.printf("NAME START:TIME END:TIME DURATION %n");
// Loop through the Set of runners and print them out
for (Runner r : set) {
System.out.printf("%s ", r.getName());
r.displayStartTime();
r.displayEndTime();
r.displayduration();
System.out.printf(" %n");
}
System.out.print("If you would like to stop enter -1 or add another runner press 1 ");
response = input.nextInt();
System.out.printf(" Response is %d ", response);
} catch (InputMismatchException inputMismatchException) {
System.out.print("Enter a number next time please! ");
}
}
}
}
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class Runner {
private String name;
private final LocalTime startTime;
private final LocalTime endTime;
private final Duration duration;
private int place;
public Runner(int place, String name, LocalTime startTime, LocalTime endTime, Duration duration) {
this.place = place;
this.name = name;
this.startTime = startTime;
this.endTime = endTime;
this.duration = duration;
}
public void displayduration() {
System.out.printf("%d Hour(s):%d Minute(s):%d Second(s) ", duration.get(ChronoUnit.HOURS),
duration.get(ChronoUnit.MINUTES), duration.get(ChronoUnit.SECONDS));
}
public void displayStartTime() {
System.out.printf("%d:%d:%d ", startTime.getHour(),
startTime.getMinute(), startTime.getSecond());
}
public void displayEndTime() {
System.out.printf("%d:%d:%d ", endTime.getHour(),
endTime.getMinute(), endTime.getSecond());
}
public String getName() {
return name;
}
public LocalTime getStartTime() {
return startTime;
}
public LocalTime getEndTime() {
return endTime;
}
public Duration getDuration() {
return duration;
}
public int getPlace() {
return place;
}
}