DateConversion程序将用户输入的日期格式转换为另一种格式。该程序运行良好,输入格式为mm / dd / yyyy,但如果用户输入1999年1月28日而不是01/28/1999,程序仍应显示1999年1月28日。可以进行哪些修改。任何帮助都会受到欢迎! 代码:
import java.util.Scanner; //Importing java Scanner
public class DateConversion {
public static void main (String[] args){ // Main method
String enteredDate; // Declaring a string
char answer = 'Y'; // Assigning a character value to variable answer
String response; // Declaring a string for user's response
Scanner keyboard = new Scanner (System.in); // Scanner object
do { // A do-while loop used to check and convert the date format if user enters y otherwise terminate
System.out.println("Please enter a date in mm/dd/yyyy format: "); // Asking for user input
enteredDate = keyboard.nextLine(); // Taking the user's input
// If, else if loops are used to check the months entered, convert and display them in another format
// This is done by checking characters at various positions
if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '1'){
System.out.println("Display date in other format: ");
System.out.println("January " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '2' ){
System.out.println("Display date in other format: ");
System.out.println("February " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '3' ){
System.out.println("Display date in other format: ");
System.out.println("March " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '4' ){
System.out.println("Display date in other format: ");
System.out.println("April " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '5' ){
System.out.println("Display date in other format: ");
System.out.println("May " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '6' ){
System.out.println("Display date in other format: ");
System.out.println("June " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '7' ){
System.out.println("Display date in other format: ");
System.out.println("July " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '8' ){
System.out.println("Display date in other format: ");
System.out.println("August " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '9' ){
System.out.println("Display date in other format: ");
System.out.println("September " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '0' ){
System.out.println("Display date in other format: ");
System.out.println("October " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '1' ){
System.out.println("Display date in other format: ");
System.out.println("November " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '2' ){
System.out.println("Display date in other format: ");
System.out.println("December " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
}
System.out.println(); // Used for line break
System.out.printf("Do you want to continue (y/n): "); // Asking the user if they want to continue
response = keyboard.nextLine(); // User's response
answer = response.charAt(0); // Getting the character at 0th position
} while (Character.toUpperCase (answer) == 'Y'); // Checking if the character obtained is y
}
}
答案 0 :(得分:-2)
如果String.split
在范围内,请使用它将用户输入的日期字符串拆分为月份,日期和年份。例如,如果用户打算在1月份,月份可能会String
或1
为01
。
String[] splitDate = enteredDate.split("/");
这将为您提供三个字符串的数组。您可能需要检查数组的长度是否为3,如果没有则发出错误消息(如果它更短或更长)。因此,您可以将月份数字符串作为splitDate[0]
等访问。如果您有疑问,请阅读数组。
如果不允许String.split
,请使用String.substring
将月份取消为{1}}或1
作为1月份。这需要您知道斜杠的位置。使用01
或indexOf
查找。白天和同年也一样。
如果您知道charAt
语句,请启用您刚刚获得的月份字符串。案例前可能有多个案例标签,例如:
switch
(另一种选择是 switch (monthNumberString) {
case "01":
case "1":
System.out.print("January");
break;
case "02":
case "2":
System.out.print("February");
break;
// Etc. for other months
}
用于保存从数字字符串到月份名称的映射,但我认为这也超出了界限。)
如果你不知道Map
(或者不允许使用它),你仍然可以在if语句中使用month子字符串:
switch
轻微的建议:
if (monthNumberString.equals("01") || monthNumberString.equals("1"))
部分(如果使用else
语句,则使用default
个案例)打印有关无法识别的月份的消息。switch
和enteredDate
可以在response
循环中声明,您首先使用它们,以便更容易在代码中找到解决方法。