【JAVA】(vip)藍橋杯試題 基礎練習 報時助手 BASIC-26 JAVA

試題 基礎練習 報時助手

資源限制
時間限制:1.0s 內存限制:512.0MB

問題描述
  給定當前的時間,請用英文的讀法將它讀出來。
  時間用時h和分m表示,在英文的讀法中,讀一個時間的方法是:
  如果m爲0,則將時讀出來,然後加上“o’clock”,如3:00讀作“three o’clock”。
  如果m不爲0,則將時讀出來,然後將分讀出來,如5:30讀作“five thirty”。
  時和分的讀法使用的是英文數字的讀法,其中0~20讀作:
  0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty。
  30讀作thirty,40讀作forty,50讀作fifty。
  對於大於20小於60的數字,首先讀整十的數,然後再加上個位數。如31首先讀30再加1的讀法,讀作“thirty one”。
  按上面的規則21:54讀作“twenty one fifty four”,9:07讀作“nine seven”,0:15讀作“zero fifteen”。

輸入格式
  輸入包含兩個非負整數h和m,表示時間的時和分。非零的數字前沒有前導0。h小於24,m小於60。

輸出格式
  輸出時間時刻的英文。

樣例輸入
0 15

樣例輸出
zero fifteen

要點

題目給出的關鍵字是字符串和條件判斷,本題沒難點
在這裏插入圖片描述

代碼

import java.util.Scanner;

public class TimeReportingAssistant {
    static String[] strArray = {"zero", "one", "two", "three", "four", "five",
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 輸入包含兩個非負整數h和m,表示時間的時和分。
        // 非零的數字前沒有前導0。h小於24,m小於60。
        int h = sc.nextInt();
        int m = sc.nextInt();
        sc.close();

        if (m == 0) {
            System.out.println(English(h) + " " + "o'clock");
        } else {
            System.out.println(English(h) + " " + English(m));
        }
    }

    public static String English(int x) {
        String x1;
        if (x <= 20) {
            x1 = strArray[x];
        } else if (x < 30) {
            x1 = "twenty" + " " + strArray[x % 20];
        } else if (x == 30) {
            x1 = "thirty";
        } else if (x < 40) {
            x1 = "thirty" + " " + strArray[x % 30];
        } else if (x == 40) {
            x1 = "forty";
        } else if (x < 50) {
            x1 = "forty" + " " + strArray[x % 40];
        } else if (x == 50) {
            x1 = "thirty";
        } else {
            x1 = "fifty" + " " + strArray[x % 50];
        }

        return x1;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章