SDUTJAVAlab3.8

简介: SDUTJAVAlab3.8

7-8 sdut-JAVA-Convert 12 To 24 Hour Clock

分数 12

 

全屏浏览

 

切换布局

作者 马新娟

单位 山东理工大学

Write a program that accepts a time on a 12-hour clock (using 3 integer variables, the first denoting hours, the second denoting minutes and the third is a designator(1 for AM, 2 for PM)). Your program should convert this input to its equivalent time as it would appear on a 24-hour clock and display the result. In your output it is only necessary to display hours and minutes.

If your hours input is less than 0 or more than 12,display the message “Value for hours must be in the range 1 to 12"; If your minutes input is less than 0 or more than 59,display the message "Value for minutes must be in the range 0 to 59"; your designator input is less than 1 or more than 2,display the message "Designator must be 1 (for AM) or 2 (for PM)".

Input Specification:

accepts a time on a 12-hour clock (using 3 integer variables, the first denoting hours, the second denoting minutes and the third is a designator).

Output Specification:

Display a 24-hour clock.

Sample Input:

12
60

Sample Output:

Value for minutes must be in the range 0 to 59

Sample Input:

12
0
1

Sample Output:

00:00

Sample Input:

12
0
2

Sample Output:

12:00

Sample Input:

8
20
1

Sample Output:

08:20

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

import java.util.*;
public class Main
{
  public static void main(String [] args)
  {
  int hours, minutes, designator;
  String r = "";
  String msg1 = "Value for hours must be in the range 1 to 12"; 
  String msg2 = "Value for minutes must be in the range 0 to 59"; 
  String msg3 = "Designator must be 1 (for AM) or 2 (for PM)";
  Scanner in =new Scanner(System.in);
  hours = Integer.parseInt(in.nextLine());
  if (hours < 1 || hours > 12)
    r = msg1;
  else
  {
    minutes = Integer.parseInt(in.nextLine());
    if (minutes < 0 || minutes > 59)
     r = msg2;
    else
    {
      designator = Integer.parseInt(in.nextLine());
      if (designator < 1 || designator > 2)
        r = msg3;
        else     
      {
      if (hours == 12 && minutes == 0 && designator == 1)
        hours = 0;
      else if (hours == 12 && designator == 1)
        hours -= 12;
      else if (hours == 12 && designator == 2)
        hours = 12;
      else if (designator == 2)
            hours += 12;      
          if (hours < 10)    r += "0" + hours;
      else               r += hours;
          if (minutes < 10)  r += ":0" + minutes; 
      else               r += ":" + minutes;
        }
      }
  }
  System.out.println(r);
  }     
}

 

 


目录
相关文章
|
8月前
|
机器学习/深度学习 人工智能 算法
2024 蓝桥杯本科B组试题
2024 蓝桥杯本科B组试题
144 0
|
8月前
|
运维 安全 前端开发
参与征文赢面试绿通资格!寻找热爱技术创作的你
发布征文,SHOW出你的故事,赢取面试绿通资格、官方流量权益、数码礼包、定制T恤等重重豪礼!
2991 305
|
8月前
QT去除QString字符串中空格的方法
QT去除QString字符串中空格的方法
281 0
|
8月前
|
Rust Java 开发者
5月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区5月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
2440 151
|
8月前
|
Perl
awk复杂用法模式匹配与动作
awk复杂用法模式匹配与动作
116 2
|
8月前
|
计算机视觉 Python
怎么使用Python轻松打造淘宝主图视频生成神器
怎么使用Python轻松打造淘宝主图视频生成神器
140 0
|
开发工具
新人乘风者礼品兑换指南
仅限2023年11月15日(含11月15日)后入驻博主用于兑换礼品,此前完成入驻的博主按原邮寄方式进行。
4441 9
|
XML Java API
Android 沉浸式状态栏必知必会
Android 沉浸式状态栏追根究底
844 0
|
运维 网络协议 测试技术
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--网络配置选项+章节实验
第六章 管理OPENSTACK网络--网络配置选项+章节实验
582 1
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--网络配置选项+章节实验
|
NoSQL Oracle Java
Eureka比Zookeeper好在哪里|学习笔记
快速学习 Eureka 比 Zookeeper 好在哪里
Eureka比Zookeeper好在哪里|学习笔记