Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java,我如何在使用模运算后找到时间?_Java_Time_Modulo - Fatal编程技术网

使用Java,我如何在使用模运算后找到时间?

使用Java,我如何在使用模运算后找到时间?,java,time,modulo,Java,Time,Modulo,我正在建立一个跑步者和他们的时间表。我需要用模计算前一个跑步者落后的时间,以分和秒为单位。前两条记录是 Runner: 198 Minutes: 29 Seconds: 05 Runner: 419 Minutes: 30 Seconds: 01 我只需要知道用模求分/秒(在单独的变量中)的公式。有人能帮忙吗 int time1=nbMinutes1*60+nbSeconds1; int time2=nbMinutes2*60+nbSeconds2; int differenceInMinu

我正在建立一个跑步者和他们的时间表。我需要用模计算前一个跑步者落后的时间,以分和秒为单位。前两条记录是

Runner: 198 Minutes: 29 Seconds: 05 Runner: 419 Minutes: 30 Seconds: 01 我只需要知道用模求分/秒(在单独的变量中)的公式。有人能帮忙吗

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;
    }

你这里的问题只与知道如何使用模数有关。您有一个主函数在做一大堆事情:初始化变量、打开文件、遍历行以及计算显示参数。这就是所谓的过程编程,而且是糟糕的过程编程:您希望在这里利用面向对象编程

//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";
  }
}
//注意有关类名的标准:将类名大写!
//另外,选择能清楚说明你在做什么的名字。
公共类显示时间
{
数据输入流输入;
//这实际上不是正确的方法,但对于本例来说,它已经足够轻量级了
List runnerIds=new ArrayList();
List runnerMinutes=new ArrayList();
List runnerSeconds=new ArrayList();
//请注意,您的主方法不应抛出异常。任何东西都无法捕获它!!
//还要注意,这只是一个入口点;它可能不应该做任何工作
公共静态void main(字符串[]args)
{
DisplayTimes display=newdisplaytimes(“c:\\java\\chapter13\\sheet2\\program2.jdat2”);
display.readAndDisplay();
}
//构造函数是初始化对象变量的正确方法
公共显示时间(字符串输入文件){
//看看下一行读起来有多容易?
this.in=newdatainputstream(newfileinputstream(fileName));
}
public void readAndDisplay(){
processData();//将处理卸载到其他地方
System.out.println(“Runner\tTotal\tTotal\tTime”);
System.out.println(“Number\tMinutes\tSeconds\tBehind\n”);

对于(int idx=0;idx++;idxUse
minutes*60*1000+seconds*1000+millis
以毫秒为单位获取时间,然后除以
60*1000
以分钟为单位和
1000
以秒为单位进行转换!!!!你至少知道一整分钟有多少秒吗?我“必须”使用模数。这是课堂作业,我不知道关于如何使用模数,我的老师不介意问关于堆栈溢出的问题,我试着用谷歌搜索,但几乎所有的东西都被阻塞了。你已经得到了公式,让我们看看你至少试着把它们应用到你的代码中,看在上帝的份上。请证明你并不懒惰,并展示你努力的成果。@PeterLawrey我知道这是我的错当除法整数时,odulus得到除法的余数。我不太清楚如何在我的循环中用我得到的代码模板来实现这一点。我将更新这个问题,以包括我的code@BrandonDurst:为什么不先尝试在循环中使用此处所示的模数,然后再将家庭作业代码提交给我们。此作业仅适用于crea读取数据,模数部分很简单,这只是我一直坚持的特定程序。我已经创建了数据文件,我正在很好地读取它,这就是本章的内容
//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";
  }
}