如何将之前输入的分秒相加

  • 本文关键字: java java-time duration
  • 更新时间 :
  • 英文 :


我正试图弄清楚如何将用户输入的分钟和秒加起来。我不是从当前时间添加,所以我知道我需要一个循环,将所有输入的分钟和秒加起来。我还想把加到60的秒数加到分钟数上,而不是一堆秒数。下面是我的代码:

import java.util.*;
public class timeAdder {
public static void main(String[] args) {
Scanner keyb = new Scanner (System.in);
System.out.println ("Welcome to the Time Adder!n ");
System.out.println("Enter 5 time durations in minutes and secondsn ");

System.out.print ("Duration 1, how many minutes? ");
int min = keyb.nextInt();
System.out.print ("Duration 1, how many seconds? ");
int sec = keyb.nextInt();
System.out.print ("Duration 2, how many minutes? ");
min=keyb.nextInt();
System.out.print ("Duration 2, how many seconds? ");
sec= keyb.nextInt();
System.out.print ("Duration 3, how many minutes? ");
min = keyb.nextInt();
System.out.print ("Duration 3, how many seconds? ");
sec = keyb.nextInt();
System.out.print ("Duration 4, how many minutes? ");
min = keyb.nextInt();
System.out.print ("Duration 4, how many seconds? ");
sec = keyb.nextInt();
System.out.print ("Duration 5, how many minutes? ");
min = keyb.nextInt();
System.out.print ("Duration 5, how many seconds? ");
sec = keyb.nextInt();
System.out.println("The total of the 5 times is: n");         
}
}

我建议您使用基于ISO-8601标准建模的java.time.Duration,它是在Java-8中作为JSR-310实现的一部分引入的。在Java-9中引入了一些更方便的方法。

import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
Duration duration = Duration.ZERO;
System.out.println("Welcome to the Time Adder!n ");
System.out.println("Enter 5 time durations in minutes and secondsn ");
System.out.print("Duration 1, how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration 1, how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
System.out.print("Duration 2, how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration 2, how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
System.out.print("Duration 3, how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration 3, how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
System.out.print("Duration 4, how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration 4, how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
System.out.print("Duration 5, how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration 5, how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
// In default format
System.out.println(duration);
// Custom format
// ####################################Java-8####################################
String formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHours() % 24,
duration.toMinutes() % 60, duration.toSeconds() % 60);
System.out.println("The total of the 5 times is: " + formattedElapsedTime);
// ##############################################################################
// ####################################Java-9####################################
formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart());
System.out.println("The total of the 5 times is: " + formattedElapsedTime);
// ##############################################################################
}
}

示例运行:

Welcome to the Time Adder!

Enter 5 time durations in minutes and seconds

Duration 1, how many minutes? 5
Duration 1, how many seconds? 10
Duration 2, how many minutes? 15
Duration 2, how many seconds? 20
Duration 3, how many minutes? 25
Duration 3, how many seconds? 30
Duration 4, how many minutes? 35
Duration 4, how many seconds? 40
Duration 5, how many minutes? 45
Duration 5, how many seconds? 50
PT2H7M30S
The total of the 5 times is: 02:07:30
The total of the 5 times is: 02:07:30

进一步改善:

我建议您对五组输入使用一个循环。

import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
Duration duration = Duration.ZERO;
System.out.println("Welcome to the Time Adder!n ");
System.out.println("Enter 5 time durations in minutes and secondsn ");
for (int i = 1; i <= 5; i++) {
System.out.print("Duration " + i + ", how many minutes? ");
duration = duration.plusMinutes(keyb.nextInt());
System.out.print("Duration " + i + ", how many seconds? ");
duration = duration.plusSeconds(keyb.nextInt());
}
// In default format
System.out.println(duration);
// Custom format
// ####################################Java-8####################################
String formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHours() % 24,
duration.toMinutes() % 60, duration.toSeconds() % 60);
System.out.println("The total of the 5 times is: " + formattedElapsedTime);
// ##############################################################################
// ####################################Java-9####################################
formattedElapsedTime = String.format("%02d:%02d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart());
System.out.println("The total of the 5 times is: " + formattedElapsedTime);
// ##############################################################################
}
}

把所有的分和秒分别加起来,然后

min+=sec/60; 
sec=sec%60;

...
System.out.print ("Duration 2, how many minutes? ");
min+=keyb.nextInt();
System.out.print ("Duration 2, how many seconds? ");
sec+= keyb.nextInt();
...
min+=sec/60; 
sec=sec%60;

相关内容

  • 没有找到相关文章

最新更新