java - 如何将数字时间转换为英文名称

标签 java

我正在开发一个程序,但在询问用户时间后它似乎停止了。就像它一起跳过 if 循环一样。请告诉我我做错了什么。

package gettimename;

import java.util.Scanner;

public class getTimeName 
{

    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the hour ");
        int hours = in.nextInt();
        System.out.println("Enter the minutes ");
        int minutes = in.nextInt();
    }


public static String getTimeName(int hours, int minutes)
{
    String time_name = "";

    String nums [] = {"Zero", "One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen", "Fifteen", "Sixteen","Seventeen","Eighteen","Nineteen",
        "Twenty","Twenty one", "Twenty two", "Twenty three", "Twenty four", "Twenty five", "Twenty six", 
        "Twenty seven", "Twenty eight", "Twenty nine"};

        if (minutes == 0)
            time_name = nums[hours] + " o'clock ";

        else if (minutes == 1)
            time_name = "one minute past " + nums[hours];

        else if (minutes == 59)
            time_name = "one minute to " + nums[(hours % 12) + 1];

        else if (minutes == 15)
            time_name = "quarter past " + nums[hours];

        else if (minutes == 30)
            time_name = "half past " + nums[hours];

        else if (minutes == 45)
            time_name = "quarter to " + nums[(hours % 12) + 1];

        else if (minutes <= 30)
            time_name = nums[minutes] + "minutes past " + nums[hours];

        else if (minutes > 30)
            time_name = nums[60 - minutes] + "minutes to " + nums[(hours % 12) + 1];



    return time_name ;
}
}

最佳答案

getTimeName()。从未在您的程序中被调用。您的程序正在做它应该做的事情,而不是执行 if 语句。

假设您的其余代码是正确的,您的 main 方法应如下所示;

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the hour ");
    int hours = in.nextInt();
    System.out.println("Enter the minutes ");
    int minutes = in.nextInt();
    System.out.println(getTimeName(hours, minutes));
}

关于java - 如何将数字时间转换为英文名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60871948/

相关文章:

java - Selenium webdriver 找不到我的按钮

java - 用正则表达式匹配两个url

java - Maven 在 Jenkins 中构建失败,在命令行上成功

java - 按升序对 Hashset 对象进行排序

java - 可与 Java 5 一起使用的最新 JGroups 版本是什么?

java - 使用 Guice : Something that isn't covered by any tutorial 进行依赖注入(inject)

java - 获取找到的序列前后有n个字符的子字符串

java - 检查原语是否已设置

java - 如何根据实时情况使方法每7天同时循环一次?

java - 无法显示超出最大字符数