gpt4 book ai didi

java - 以数组形式跟踪田径运动时间

转载 作者:行者123 更新时间:2023-12-01 17:52:49 25 4
gpt4 key购买 nike

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

  1. 从A点到B点,记录出发时间和到达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 实例。您需要将它们保存在 map 或您喜欢的任何数据结构中。下面是一个测试程序,展示了系统的工作原理:

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)

关于java - 以数组形式跟踪田径运动时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48136298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com