日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java黑皮书课后题第3章:*3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数

發布時間:2024/7/23 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java黑皮书课后题第3章:*3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

*3.30(當前時間)修改編程練習題2.8,以12小時時鐘制顯示小時數

  • 題目
    • 題目概述
    • 運行示例
    • 編程練習題2.8
    • 破題
  • 代碼

題目

題目概述

*3.30(當前時間)修改編程練習題2.8,以12小時時鐘制顯示小時數

運行示例

Enter the time zone offset to GMT: -5
The current time is 4:50:34 AM

編程練習題2.8

歡迎點擊這里前往我的2.8博文,或復制url到瀏覽器:

https://blog.csdn.net/weixin_46356698/article/details/119783225

import java.util.Scanner;public class Test2_8 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter the time zone offset to GMT:");int offset = input.nextInt();// Obtain the total milliseconds since midnight, Jan 1, 1970long totalMilliseconds = System.currentTimeMillis();// Obtain the total seconds since midnight, Jan 1, 1970long totalSeconds = totalMilliseconds / 1000;totalSeconds += offset * 60 * 60;// Compute the current second in the minute in the hourlong currentSecond = totalSeconds % 60;// Obtain the total minuteslong totalMinutes = totalSeconds / 60;// Compute the current minute in the hourlong currentMinute = totalMinutes % 60;// Obtain the total hourslong totalHours = totalMinutes / 60;// Compute the current hourlong currentHour = totalHours % 24;// Display resultsSystem.out.println("Current time is " + currentHour + ":"+ currentMinute + ":" + currentSecond + " GMT");} }

破題

在理解2.8題目的基礎上只需要更改一小部分即可
我的理解,是在最后輸出前增加對小時的變換
對12求余即可獲取3.30要求的小時數
原數除12如果為0后面跟AM,如果為1后面跟PM

代碼

import java.util.Scanner;public class Test2_8 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("Enter the time zone offset to GMT:");int offset = input.nextInt();// Obtain the total milliseconds since midnight, Jan 1, 1970long totalMilliseconds = System.currentTimeMillis();// Obtain the total seconds since midnight, Jan 1, 1970long totalSeconds = totalMilliseconds / 1000;totalSeconds += offset * 60 * 60;// Compute the current second in the minute in the hourlong currentSecond = totalSeconds % 60;// Obtain the total minuteslong totalMinutes = totalSeconds / 60;// Compute the current minute in the hourlong currentMinute = totalMinutes % 60;// Obtain the total hourslong totalHours = totalMinutes / 60;// Compute the current hourlong currentHour = totalHours % 24;long currentHour_12 = currentHour % 12;// Display resultsif(currentHour / 12 == 0)System.out.println("Current time is " + currentHour_12 + ":"+ currentMinute + ":" + currentSecond + " AM");elseSystem.out.println("Current time is " + currentHour_12 + ":"+ currentMinute + ":" + currentSecond + " PM");} }

總結

以上是生活随笔為你收集整理的Java黑皮书课后题第3章:*3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。