【问题标题】:tracking athletics exercise time in array跟踪数组中的田径运动时间
【发布时间】:2018-01-07 10:34:09
【问题描述】:

我正在构建一个 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);
    }

}

【问题讨论】:

  • departuretime[0] 值代表什么? A 和 B 之间的时间?
  • 运动左点A的时间。例如07:20
  • 您设法计算了数组的总和和平均值。这与你的问题有什么关系超出我的理解
  • 如果他在 30 分钟后到达 B 点,则应存储为 ; 07:50
  • 应该存储吗?怎么样??

标签: java arrays


【解决方案1】:

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

首先,数组并不是这里最好的数据结构。不要使用数组索引来表示应用程序域数据。数组非常适合表示相同项目的复制。如果需要 start 和 end 然后使用“start”和“end”变量,不要使用索引 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)

【讨论】:

  • 这很好用。我也喜欢你的设计理念。
【解决方案2】:

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

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点,出发时间为记录,到达时间为点 B.

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

【讨论】:

  • 为什么要使用双索引数组?尺寸代表什么?
  • @SharonBenAsher 如果您阅读 OP 问题,他提到他需要 A 和 B 的到达和离开时间。然后,从 B 到 A 再次相同。同样,此实现可以更改,因为它独立于 main类。
  • 所以?你真的认为这对我们所有人来说都是显而易见的吗?真的??我还是不明白
  • @SharonBenAsher 你不明白什么??你能具体点吗?
  • @SharonBenAsher 我有其他解决方案结合了多个数据结构,它很灵活,可以处理所有场景。但是,看 OP 问题,我明白,他需要简单而直接的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
相关资源
最近更新 更多