蓝桥杯 BASIC-26 VIP试题 报时助手
试题 基础练习 报时助手
时间用时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”。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 小时
int h = scanner.nextInt();
// 分钟
String m = scanner.next();
int M = Integer.parseInt(m);
// h小于24,m小于60
if ((h >= 0 && h < 24) && (M >= 0 && M < 60)) {
/**
* 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”。
*/
switch (h) {
case 0:
System.out.print("zero ");
break;
case 1:
System.out.print("one ");
break;
case 2:
System.out.print("two ");
break;
case 3:
System.out.print("five ");
break;
case 4:
System.out.print("four ");
break;
case 5:
System.out.print("five ");
break;
case 6:
System.out.print("six ");
break;
case 7:
System.out.print("seven ");
break;
case 8:
System.out.print("eight ");
break;
case 9:
System.out.print("nine ");
break;
case 10:
System.out.print("ten ");
break;
case 11:
System.out.print("eleven ");
break;
case 12:
System.out.print("twelve ");
break;
case 13:
System.out.print("thirteen ");
break;
case 14:
System.out.print("fourteen ");
break;
case 15:
System.out.print("fifteen ");
break;
case 16:
System.out.print("sixteen ");
break;
case 17:
System.out.print("seventeen ");
break;
case 18:
System.out.print("eighteen ");
break;
case 19:
System.out.print("nineteen ");
break;
case 20:
System.out.print("twenty ");
break;
case 21:
System.out.print("twenty one ");
break;
case 22:
System.out.print("twenty two ");
break;
case 23:
System.out.print("twenty three ");
break;
}
// 分钟
switch (M) {
case 00:
System.out.print("o'clock");
break;
case 01:
System.out.print("one");
break;
case 02:
System.out.print("two");
break;
case 03:
System.out.print("five");
break;
case 04:
System.out.print("four");
break;
case 05:
System.out.print("five");
break;
case 06:
System.out.print("six");
break;
case 07:
System.out.print("seven");
break;
case 8:
System.out.print("eight");
break;
case 9:
System.out.print("nine");
break;
case 10:
System.out.print("ten");
break;
case 11:
System.out.print("eleven");
break;
case 12:
System.out.print("twelve");
break;
case 13:
System.out.print("thirteen");
break;
case 14:
System.out.print("fourteen");
break;
case 15:
System.out.print("fifteen");
break;
case 16:
System.out.print("sixteen");
break;
case 17:
System.out.print("seventeen");
break;
case 18:
System.out.print("eighteen");
break;
case 19:
System.out.print("nineteen");
break;
case 20:
System.out.print("twenty");
break;
case 21:
System.out.print("twenty one");
break;
case 22:
System.out.print("twenty two");
break;
case 23:
System.out.print("twenty three");
break;
case 24:
System.out.print("twenty four");
break;
case 25:
System.out.print("twenty five");
break;
case 26:
System.out.print("twenty six");
break;
case 27:
System.out.print("twenty seven");
break;
case 28:
System.out.print("twenty eight");
break;
case 29:
System.out.print("twenty nine");
break;
case 30:
System.out.print("thirty");
break;
case 31:
System.out.print("thirty one");
break;
case 32:
System.out.print("thirty two");
break;
case 33:
System.out.print("thirty three");
break;
case 34:
System.out.print("thirty four");
break;
case 35:
System.out.print("thirty five");
break;
case 36:
System.out.print("thirty six");
break;
case 37:
System.out.print("thirty seven");
break;
case 38:
System.out.print("thirty eight");
break;
case 39:
System.out.print("thirty nine");
break;
case 40:
System.out.print("forty");
break;
case 41:
System.out.print("forty one");
break;
case 42:
System.out.print("forty two");
break;
case 43:
System.out.print("forty three");
break;
case 44:
System.out.print("forty four");
break;
case 45:
System.out.print("forty five");
break;
case 46:
System.out.print("forty six");
break;
case 47:
System.out.print("forty seven");
break;
case 48:
System.out.print("forty eight");
break;
case 49:
System.out.print("forty nine");
break;
case 50:
System.out.print("fifty");
break;
case 51:
System.out.print("fifty one");
break;
case 52:
System.out.print("fifty two");
break;
case 53:
System.out.print("fifty three");
break;
case 54:
System.out.print("fifty four");
break;
case 55:
System.out.print("fifty five");
break;
case 56:
System.out.print("fifty six");
break;
case 57:
System.out.print("fifty seven");
break;
case 58:
System.out.print("fifty eight");
break;
case 59:
System.out.print("fifty nine");
break;
}
}
}
}
方法二,较为简单
采用整除拼接的方法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in=new Scanner(System.in);
//接收小时
int hour=in.nextInt();
//接收分钟
int min=in.nextInt();
//记录0-20无规律英文
String[] arr= {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen",
"noneteen","twenty"};
//小时在20以内
if(hour<=20)
{
System.out.print(arr[hour]);
}
else
{
//21-24之间
System.out.print(arr[20]+" "+arr[hour%10]);
}
//分钟等于0的情况
if(min==0)
{
System.out.println(" "+"o'clock");
}
//分钟在0-20之间
if(min<=20&&min>0)
{
System.out.print(" "+arr[min]);
}
else if(min/10>=2)
{
//整十的情况
if(min/10==2)
{
System.out.print(" "+"twenty");
}
if(min/10==3)
{
System.out.print(" "+"thirty");
}
if(min/10==4)
{
System.out.print(" "+"forty");
}
if(min/10==5)
{
System.out.print(" "+"fifty");
}
//一位数的情况
System.out.print(" "+arr[min%10]);
}
}
}
演示效果如下
希望本篇文章能够帮助大家!
谢谢大家!
- 点赞
- 收藏
- 关注作者
评论(0)