SDUT JAVAlab3.8(法2)

简介: SDUT JAVAlab3.8(法2)

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.Scanner;
public class Main
{
  public static void main(String [] args)
  {
    Scanner in=new Scanner(System.in);
    int t1,t2,t3;
      t1=in.nextInt();
    if(t1<0||t1>12)
    {
      System.out.println("Value for hours must be in the range 1 to 12");
      return;
    }
      t2=in.nextInt();
    if(t2<0||t2>59)
    {
      System.out.println("Value for minutes must be in the range 0 to 59");
      return;
    }
    t3=in.nextInt();
    if(t3<1||t3>2)
    {
      System.out.println("Designator must be 1 (for AM) or 2 (for PM)");
      return;
    }
    if(t1==12&&t3==2)
    {
      t1=12;
    }
        else if(t1==12&&t3==1)
        {
            t1=0;
        }
    else if(t3==2)
    {
      t1=t1+12;
    }
    System.out.println( String.format("%02d", t1) + ":" +String.format("%02d", t2));
  }
}


目录
相关文章
|
2月前
sdut 链表7
sdut 链表7
16 0
|
2月前
SDUT 链表9-------7-9 sdut-C语言实验-约瑟夫问题
SDUT 链表9-------7-9 sdut-C语言实验-约瑟夫问题
19 0
|
2月前
7-5 sdut-C语言实验-最少拦截系统
7-5 sdut-C语言实验-最少拦截系统
25 6
|
2月前
|
算法
7-2 sdut-C语言实验-数字三角形问题
7-2 sdut-C语言实验-数字三角形问题
14 1
|
2月前
7-4 sdut-C语言实验-区间覆盖问题
7-4 sdut-C语言实验-区间覆盖问题
22 2
|
2月前
7-2 sdut-C语言实验-删数问题
7-2 sdut-C语言实验-删数问题
18 2
|
2月前
|
机器学习/深度学习 安全
7-3 sdut-C语言实验-王老师去爬山
7-3 sdut-C语言实验-王老师去爬山
24 1
|
2月前
sdut 实验3.9---------7-9 sdut-C语言实验-蟠桃记 分数 20
sdut 实验3.9---------7-9 sdut-C语言实验-蟠桃记 分数 20
15 0
|
2月前
sdut 链表10------7-10 sdut-C语言实验-不敢死队问题
sdut 链表10------7-10 sdut-C语言实验-不敢死队问题
15 0
|
2月前
7-8 sdut-C语言实验-装船问题分数
7-8 sdut-C语言实验-装船问题分数
17 0