我正在开发一个程序,但在询问用户时间后它似乎停止了。就像它一起跳过 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/