我正在实施一个程序,该程序从输入英文名称的时间点返回时间。例如,“十分钟,过去两点”或“三点半”。该程序必须提示用户一次。
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;
}
}
答案 0 :(得分:1)
好的,我现在就做对了。
public class TimeToEnglish{
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();
String sHours = sTime.substring(0,2);
String sMinutes = sTime.substring(3);
try{ int hours = Integer.parseInt(sHours);
int minutes = Integer.parseInt(sMinutes);
System.out.println("Time is: "+getTimeName(hours, minutes));
}
catch( NumberFormatException e){
System.out.println("invalid input");//affiche une erreur ici
}
}
public static String getTimeName(int hours, int minutes){
String time_name = "";
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)
time_name = hour_mint[hours]+"o'clock";
else if (minutes==15)
time_name = "Quarter past "+hour_mint[hours];
else if (minutes==30)
time_name = "Quarter past "+hour_mint[hours];
else if (minutes==45)
time_name = "Quarter to"+a;
else if (minutes<30) // for minutes between 1-29
time_name = hour_mint[minutes]+" past "+hour_mint[hours];
else // between 31-59
time_name = hour_mint[60-minutes]+" to "+a;
}
else
time_name = "invalid time format";
return time_name;
}
}
答案 1 :(得分:0)
所以对于回归问题。你可以写
public static String[] getTimeName(int hours, int minutes) {
返回数组。 当时我不确定你想做什么,但是你可以读取字符串然后拆分为&#34;:&#34;。然后你有两个单独的值,可以解析为int。