【发布时间】:2016-02-15 19:26:05
【问题描述】:
我正在实现一个程序,该程序从输入的英文名称返回某个时间点的时间。例如,“两点十分钟”或“三点半”。程序必须提示用户一次。
System.out.println("time is hours:minutes")
用户不应输入小时,然后分别输入分钟。输入必须是小时冒号分钟。对于方法体,我在数组返回时收到编译错误。
public class TimeLab {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Date d = new Date();
SimpleDateFormat SDF = new SimpleDateFormat("hh:mm");
System.out.println("Enter the time");
String sTime = SDF.format(d).toString();
sTime = in .nextLine();
int hours = Integer.parseInt(sTime);
int minutes = Integer.parseInt(sTime);
// output the current name of time, hours, minutes.
// System.out.println("Current hours:minutes is " + sTime);
System.out.println("Time is: " + getTimeName(hours, minutes));
// prompt user for hours and minutes, make sure that the user is
// notified we are using 12 hour time
// filter against anything <1 and >12 for hours, <0 and >60 for minutes
//
// output the name of the imput time.
}
public static String getTimeName(int hours, int minutes) {
if ((hours >= 1 && hours <= 12) && (minutes >= 0 && minutes <= 59)) {
String hour_mint[] = {
"",
"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"
};
String a;
if (hours == 12)
a = hour_mint[1]; // put 'one' if hour is 12
else
a = hour_mint[hours + 1]; // if hour is not 12 then store an hour ahead of given hour
System.out.print("time is : " + hours + ":" + minutes + ".");
if (minutes == 0)
System.out.println(hour_mint[hours] + "o'clock");
else if (minutes == 15)
System.out.println("Quarter past " + hour_mint[hours]);
else if (minutes == 30)
System.out.println("Half past" + hour_mint[hours]);
else if (minutes == 45)
System.out.println("Quarter to" + a);
else if (minutes < 30) // for minutes between 1-29
System.out.println(hour_mint[minutes] + " " + "past" + hour_mint[hours]);
else // between 31-59
System.out.println(hour_mint[60 - minutes] + " " + "to " + a);
} else
System.out.println("invalid time ");
//String hour_mint = null;
return hour_mint;
}
}
【问题讨论】:
-
修复您的代码格式。另外,您的问题到底是什么?
-
首先你如何从一个方法返回一个字符串数组?我有一个编译错误。然后对于主类,我不确定如何使用子字符串以这种格式提示用户时间“HH:MM”
-
似乎如果您遇到编译器错误,您可能应该分享该特定错误,包括行号甚至发生它的标记。