java 中的时间和小时数
Time and hours in java
import java.util.Scanner;
public class Nc3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter time of arrival: ");
double time = input.nextDouble();
int arrivalTime;
arrivalTime = 0;
arrivalTime += time;
if (time % 20 != 0) {
time %= 20;
time = 20 - time;
System.out.println(arrivalTime +time);
}
else if (((arrivalTime + time) % 100) == 60.0) {
time += 40;
System.out.println(arrivalTime + time);
}
else
System.out.println("Now - " +arrivalTime);
}
}
我被要求编写一个程序来获取当前时间的输入并返回下一辆公共汽车离开的时间知道公共汽车每 20 分钟在 00、20、40 离开,问题是如果我输入代表 22:43 的 2243,我将返回 2260 而不是 2300
java.time
我推荐你使用java.time.Duration
which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation。
演示:
import java.time.Duration;
import java.time.LocalTime;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter time of arrival: ");
showNextDepartureTime(input.nextLine());
}
static void showNextDepartureTime(String arrivalTime) {
Duration nextDeparture = Duration.between(LocalTime.MIN, LocalTime.parse(arrivalTime));
int remainder = nextDeparture.toMinutesPart() % 20;
if (remainder != 0)
nextDeparture = nextDeparture.minusMinutes(remainder).plusMinutes(20);
System.out.printf("The next departure is at %2d:%02d", nextDeparture.toHoursPart(),
nextDeparture.toMinutesPart());
}
}
样本运行:
Enter time of arrival: 22:43
The next departure is at 23:00
另一个样本运行:
Enter time of arrival: 22:00
The next departure is at 22:00
更新
正如 Lino 在他的 中所建议的,您可以按如下方式替换 if
正文:
if (remainder != 0)
nextDeparture = nextDeparture.plusMinutes(20 - remainder);
import java.util.Scanner;
public class Nc3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter time of arrival: ");
double time = input.nextDouble();
int arrivalTime;
arrivalTime = 0;
arrivalTime += time;
if (time % 20 != 0) {
time %= 20;
time = 20 - time;
System.out.println(arrivalTime +time);
}
else if (((arrivalTime + time) % 100) == 60.0) {
time += 40;
System.out.println(arrivalTime + time);
}
else
System.out.println("Now - " +arrivalTime);
}
}
我被要求编写一个程序来获取当前时间的输入并返回下一辆公共汽车离开的时间知道公共汽车每 20 分钟在 00、20、40 离开,问题是如果我输入代表 22:43 的 2243,我将返回 2260 而不是 2300
java.time
我推荐你使用java.time.Duration
which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation。
演示:
import java.time.Duration;
import java.time.LocalTime;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter time of arrival: ");
showNextDepartureTime(input.nextLine());
}
static void showNextDepartureTime(String arrivalTime) {
Duration nextDeparture = Duration.between(LocalTime.MIN, LocalTime.parse(arrivalTime));
int remainder = nextDeparture.toMinutesPart() % 20;
if (remainder != 0)
nextDeparture = nextDeparture.minusMinutes(remainder).plusMinutes(20);
System.out.printf("The next departure is at %2d:%02d", nextDeparture.toHoursPart(),
nextDeparture.toMinutesPart());
}
}
样本运行:
Enter time of arrival: 22:43
The next departure is at 23:00
另一个样本运行:
Enter time of arrival: 22:00
The next departure is at 22:00
更新
正如 Lino 在他的 if
正文:
if (remainder != 0)
nextDeparture = nextDeparture.plusMinutes(20 - remainder);