if(ExcessTimeHours >= 24) {
ExcessTimeHours = 0;
}
if(ExcessTimeMin >= 60) {
ExcessTimeMin = 0;
ExcessTimeHours ;
}
我在第一部電影開始時添加時間,然后添加那部電影的時長,然后在第二部電影開始時添加,還添加這部電影的時長,程式會告訴我是否可以同時觀看兩部電影問題,或者我是否會錯過電影的幾分鐘或我無法制作它。
我的問題是,當時間超過午夜并從 24 小時跳到 0 小時時,我不知道如何解決這個問題。搞不清楚狀況。
System.out.println("Start of movie A:");
System.out.print("hour: "); // 17; 23; 0
int startHourA = s.nextInt();
System.out.print("min: "); // 30; 30; 30
int startMinA = s.nextInt();
System.out.println("Length of movie A:");
System.out.print("hour: "); // 2; 2; 2
int lengthHourA = s.nextInt();
System.out.print("min: "); // 0; 0; 1
int lengthMinA = s.nextInt();
System.out.println("Start of movie B:");
System.out.print("hour: "); // 20; 0; 0
int startHourB = s.nextInt();
System.out.print("min: "); // 0; 20; 31
int startMinB = s.nextInt();
System.out.println("Length of movie B:");
System.out.print("hour: "); // 0; 1; 2
int lengthHourB = s.nextInt();
System.out.print("min: "); // 35; 35; 0
int lengthMinB = s.nextInt();
int timeBetweenMoviesHour = startHourB - ExcessTimeHours;
int timeBetweenMoviesMin = startMinB - ExcessTimeMin;
if (timeBetweenMoviesHour == 0 && timeBetweenMoviesMin >= 0) {
System.out.println("Recommendation: No problem");
}
else if (timeBetweenMoviesMin > 0) {
System.out.println("Recommendation: You won't see "
timeBetweenMoviesMin " minutes");
}
else System.out.println("Recommendation: You can't make it");
uj5u.com熱心網友回復:
時間
您將需要使用LocalTime和Duration類。例如像下面這樣。我不認為我的代碼完全符合您的要求,但它應該可以幫助您入門。
// Convert inputs to LocalTime and Duration objects
LocalTime startMovieA = LocalTime.of(16, 0);
Duration lengthMoviaA = Duration.ofHours(2).plusMinutes(15);
LocalTime startMovieB = LocalTime.of(18, 45);
// Do calculations
LocalTime endMovieA = startMovieA.plus(lengthMoviaA);
Duration timeBetweenMovies = Duration.between(endMovieA, startMovieB);
// Output
if (timeBetweenMovies.isNegative()) {
System.out.println("You will be missing " timeBetweenMovies.abs());
}
else if (timeBetweenMovies.isZero()) {
System.out.println("You will just make it");
}
else {
System.out.println("You will have " timeBetweenMovies " between the movies");
}
輸出是:
您將在電影之間擁有 PT30M
Duration物件列印有點滑稽。格式為 ISO 8601。對于您的用戶,您需要更好地格式化,例如You will have 30 minutes between the movies. 搜索如何和/或使用底部的鏈接。它在 WWW 的不同地方都有描述。
鏈接
- Oracle 教程:解釋如何使用 java.time 的日期時間。
- 維基百科文章:ISO 8601
- 關于如何格式化 a 的問題和答案
Duration:- 將秒值轉換為小時分秒?- 我的答案
- 如何在java中格式化持續時間?(例如格式 H:MM:SS) — lauhub 的回答
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371058.html
上一篇:基本的JavaHiLow猜謎游戲
