在阵列中跟踪田径运动时间



我正在构建一个java系统来记录运动者所花费的时间并将其存储在数组中。约束如下

从A点到B点,
  1. 记录出发时间,到达时间到B点。
  2. 从B点出发时间记录和到达时间到A点。
  3. 应计算点之间的时间
  4. 该练习每周 7 天进行。

我只能弄清楚如何以一种方式录制。如何跟踪每个人每天的出发和到达? 法典:

public class ArrayLesson {
static int[] departuretime = new int[7];
static int sum = 0;
static double ave = 0;
public static void main(String[] args) {
departuretime[0] = 07;
departuretime[1] = 20;
departuretime[2] = 40;
departuretime[3] = 12;
departuretime[4] = 10;
departuretime[5] = 12;
departuretime[6] = 11;
System.out.println("index      Value");//headings
for (int i = 0; i < departuretime.length; i++) {
sum = departuretime[i] + sum;
System.out.printf("%5d%8d%n", i, sum);
}
System.out.println("Weekly total:" + sum);
System.out.println("weekly average:" + sum / departuretime.length);
}
}

好的,这是我对这个问题的看法:

首先,数组不是这里最好的数据结构。 不要使用数组索引来表示应用程序域数据。 数组适用于表示相同项目的复制。 如果您需要开始和结束,请使用"开始"和"结束"变量,不要使用索引 0 和 1 来表示这一点。

其次,如果您需要记录和比较日期/时间,可以使用内置的Java类来做到这一点。 所以使用它们。 这包括时间之间的持续时间、星期几枚举等概念

所以我的解决方案如下

1)创建一个数据结构来保存单向的开始-结束记录时间。 这是计算持续时间的地方:

import java.time.*;
import java.time.format.DateTimeFormatter;
// records start and end times for one direction
public class RecordedTrack {
public LocalTime start;
public LocalTime end;
public RecordedTrack (LocalTime start, LocalTime end) {
this.start = start;
this.end = end;
}
// accept String times in "HH:MM:SS" format 
public RecordedTrack (String start, String end) {
this(
LocalTime.parse(start, DateTimeFormatter.ISO_LOCAL_TIME),
LocalTime.parse(end, DateTimeFormatter.ISO_LOCAL_TIME));
}
public Duration getDuration() {
return Duration.between(start, end);
}
// return String times in "MM:SS" format 
public String getDurationStr () {
long seconds = getDuration().getSeconds();
return String.format("%02d:%02d", (seconds % 3600) / 60, seconds % 60);
}
}

2)创建一个数据结构来保存双向完整的行程:

// records start and end times for complete two directions itinerary
public class RecordedItinerary {
public RecordedTrack AtoB;
public RecordedTrack BtoA;
public RecordedItinerary (RecordedTrack AtoB, RecordedTrack BtoA) {
this.AtoB = AtoB;
this.BtoA = BtoA;
}
}

3)创建一个数据结构来保存与一个人相关的所有数据。 我假设一个人有一个名字来区别于其他人,并且每天进行一次体育锻炼。如果需要,可以对此进行修改:

// records weekly itineraries of one person 
public class Person {
public String name;
public RecordedItinerary[] weeklyItineraries = new RecordedItinerary[DayOfWeek.values().length+1]; 
public Person (String name) {
this.name = name;
}
// add an itinerary of given day-of-week
public void addDailyItinerary(DayOfWeek dow, RecordedItinerary itinerary) {
weeklyItineraries[dow.getValue()] = itinerary;
}
// add today's itinerary
public void addDailyItinerary(RecordedItinerary itinerary) {
addDailyItinerary(LocalDateTime.now().getDayOfWeek(), itinerary);
}
// get an itinerary of given day-of-week 
public RecordedItinerary getDailyItinerary(DayOfWeek dow) {
return weeklyItineraries[dow.getValue()];
}
// get today's itinerary
public RecordedItinerary getDailyItinerary() {
return getDailyItinerary(LocalDateTime.now().getDayOfWeek());
}
// a person is identified by his/her name
// will throw ClassCastException if arg is not a Person
@Override
public boolean equals(Object o) {
return this.name.equals(((Person)o).name);
}
}

4) 现在,您可以从输入中创建Person实例。 您需要将它们保存在地图或您喜欢的任何数据结构中。 以下是显示系统工作原理的测试程序:

public class RecordingTimesSystem {
public static void main(String... args) {
// new person!
Person john = new Person("John Doe");
// add today's two-direction itinerary 
john.addDailyItinerary(
new RecordedItinerary(
new RecordedTrack("07:10:00", "07:20:30"), // a-to-b recorded times
new RecordedTrack("09:30:00", "09:37:00")) // b-to-a recorded times
);
DayOfWeek requestedDayOfWeek = LocalDateTime.now().getDayOfWeek();
System.out.println(john.name + " " + requestedDayOfWeek.toString() + 
" AtoB: " + 
john.getDailyItinerary().AtoB.getDuration().getSeconds() + " (sec) " +  
john.getDailyItinerary().AtoB.getDurationStr() + " (mm:ss)");
System.out.println(john.name + " " + requestedDayOfWeek.toString() + 
" BtoA: " + 
john.getDailyItinerary().BtoA.getDuration().getSeconds() + " (sec) " +  
john.getDailyItinerary().BtoA.getDurationStr() + " (mm:ss)");
}
}

输出:

John Doe SUNDAY AtoB: 630 (sec) 10:30 (mm:ss)
John Doe SUNDAY BtoA: 420 (sec) 07:00 (mm:ss)

您可以创建一个类,该类可以包含从 A 点和 B 点到达和离开的详细信息。然后,该类的对象可以存储在第 1 天 2..7 的Map中。

class SportsPerson
{
private String sName;  // sports person name.
private int[][] pointAtoB = new int[1][1]; // arrival and dep for A to B
private int[][] pointBtoA = new int[1][1]; // arrival and dep for B to A
// have getter and setter
} 

请注意,int可以是数组Date,可以存储完整的日期和时间。但是,在他的示例中,OP 指定了数字。

现在,在您的主类中,创建上述类的实例。

Map<Integer,SportsPerson> map = new HashMap<> ();
SportsPerson person = new SportsPerson ();
// set values
map.put (1,person); // 1 represent day 1.

编辑

正如我的一位朋友询问有关使用2D阵列的解释。原因如下。

private int[][] pointAtoB = new int[1][1];

从A点到B点,记录出发时间,到达时间到点 二.

这意味着 A 点到 B有两个条目,换句话说,A 点到 B 点的开始和结束时间。而且,2D 数组最好代表这一点。(IMO),它也可以是一维阵列。但是,要表示开始和结束时间,最好使用两列而不是一列。

最新更新