-4

我正在建立一个跑步者和他们的时间表。我需要使用模数以分钟和秒为单位找到前一个跑步者背后的时间。前两条记录是

亚军:198
分钟:29
秒数:05

亚军:419
分钟:30
秒数:01

落后一号选手的时间?

到目前为止,这是我的代码:

import java.io.*;
import java.text.*;
public class read3
{
public static void main(String[] args)throws IOException
{
    DataInputStream in=new DataInputStream(new FileInputStream("c:\\java\\chapter13\\sheet2\\program2.jdat2"));
    int id;
    int mins;
    int secs;//,num3;
    double calc=0,calc2=0;
    char chr;
    double tcalc=0;
    double t1=0,t2=0,t3=0;
    NumberFormat money=NumberFormat.getCurrencyInstance();
    System.out.println("Runner\tTotal  \tTotal  \tTime");
    System.out.println("Number\tMinutes\tSeconds\tBehind\n");
    try
    {
        while(true)
        {
            id=in.readInt();
            in.readChar();
            mins=in.readInt();
            in.readChar();
            secs=in.readInt();
            in.readChar();

            System.out.println(id+"\t      "+mins+"\t    "+secs+"\t"+calc);
        }
    }
    catch(EOFException e)
    {
        //Hi
    }
    in.close();
}
}

我只需要知道使用模找到分钟/秒(在单独的变量中)的方程。有人可以帮忙吗?

4

2 回答 2

3
int time1=nbMinutes1*60+nbSeconds1;
int time2=nbMinutes2*60+nbSeconds2;

int differenceInMinutes = (time2-time1)/60;
int differenceinSeconds = (time2-time1)%60;

编辑:要将其应用于您的代码,我将执行以下操作:

    Integer duration=null;

    while(true)
    {
        id=in.readInt();
        in.readChar();
        mins=in.readInt();
        in.readChar();
        secs=in.readInt();
        in.readChar();

        Integer newDuration=60*mins+secs;

        //duration is null for the first one.
        if(duration!=null){
          System.out.println(id+"\t      "+(newDuration-duration)/60+"\t    "+secs+"\t"+(newDuration-duration)%60);
        }

        duration = newDuration;
    }
于 2013-05-06T18:15:04.480 回答
0

您在这里的问题仅与知道如何使用模数相关。你有一个主函数在做一堆乱七八糟的事情:初始化变量、打开文件、遍历行以及计算显示参数。这被称为过程式编程,并且是糟糕的过程式编程:您想在这里利用面向对象的编程。

//note the standards regarding class names: Capitalize Class Names!
//also, pick names that make it clear what you're doing.
public class DisplayTimes 
{
  DataInputStream in;

  //This is not actually the correct way to do this, but it's lightweight enough for this example
  List<Integer> runnerIds = new ArrayList<Integer>();
  List<Integer> runnerMinutes = new ArrayList<Integer>();
  List<Integer> runnerSeconds = new ArrayList<Integer>();

  //note that your main method should not throw an exception. Nothing can catch it!!
  //also note that this is just an entry point; it probably shouldn't do any work
  public static void main(String[] args) 
  {
    DisplayTimes display = new DisplayTimes("c:\\java\\chapter13\\sheet2\\program2.jdat2");
    display.readAndDisplay();
  }

  //constructors are the proper way to initialize object variables
  public DisplayTimes(String inputFile) { 
    //see how much easier this next line is to read?
    this.in = new DataInputStream(new FileInputStream(fileName));
  }

  public void readAndDisplay() {
    processData(); //offload processing somewhere else
    System.out.println("Runner\tTotal  \tTotal  \tTime");
    System.out.println("Number\tMinutes\tSeconds\tBehind\n");

    for (int idx = 0; idx++; idx<runnerIds.size()) {
      String output = runnerIds.get(idx)+"\t";
      output = output + runnerMinutes.get(idx)+"\t";
      output = output + runnerSeconds.get(idx)+"\t";
      output = output + calculateDifference(idx)+"\t";

      System.out.println(output);
    }
  }

  private void processData() {
    boolean moreData = true;
    while (moreData) { //offload the work of the loop to another method
      moreData = processRunner();
    }
  }

  private boolean processRunner() {
    try {
      int id=in.readInt();
      in.readChar();//how is this file formatted that you need to skip a char?
      int mins=in.readInt();
      in.readChar();
      int secs=in.readInt();

      //Here you should really sanity-check your values
      //Instead we'll just store them
      this.runnerIds.add(id);
      this.runnerMinutes(mins);
      this.runnerSeconds(secs);

      return isFinished();
    } catch (EOFException e) {
      //Exceptions should NOT be used for program control. 
      //They should be used when there is bad data.
      System.out.println("There was an unexpected termination of the datafile.");
    }

  }

  private boolean isFinished() {
    in.mark(1);
    if (in.read == -1) {//checks cleanly if we're at the end of the file.
      return false;
    } else {
      in.reset();//moves you back to mark
      return true;
    }
  }

  private String calculateDifference(int idx) {
    if (idx == 0) { return "\t"; } //First runner is first!
    int previousTimeInSeconds = (runnerMinutes.get(idx-1) * 60) + runnerSeconds.get(idx-1);
    int nextTimeInSeconds = (runnerMinutes.get(idx) * 60) + runnerSeconds.get(idx);

    int delta = (nextTimeInSeconds - previousTimeInSeconds);
    return (delta / 60) + "min " + (delta % 60) + "sec \t";
  }
}

您应该在这里带走的主要内容是您来到这里的问题 - 计算跑步者 a 和 b 之间的差异 - 只是您提供的代码的一小部分。您无法提供简短、自包含、正确(可编译)的示例,因为您的代码与其他代码不正确地纠缠在一起。通过编写干净的代码,您可以专注于calculateDifference()您实际需要帮助的一个功能 ( )。虽然我知道您可能正在开始并完成一本书(基于所介绍的内容),但您可以学到的最基本的东西是如何将问题分解成尽可能小的部分。利用语言来帮助你;不要与之抗争。

于 2013-05-06T20:17:28.820 回答