Java 我必须拆分用户以DDMMYYYY格式输入的日期。但是,如果您输入70702000,它将显示我的所有3个错误,而不是一个
代码如下:Java 我必须拆分用户以DDMMYYYY格式输入的日期。但是,如果您输入70702000,它将显示我的所有3个错误,而不是一个,java,switch-statement,break,boolean-expression,Java,Switch Statement,Break,Boolean Expression,代码如下: { System.out.println("Please enter the date you would like to split"); System.out.println("Please make sure it is in the format DDMMYYYY"); date = sc.nextInt(); day = date / 10000
{
System.out.println("Please enter the date you would like to split");
System.out.println("Please make sure it is in the format DDMMYYYY");
date = sc.nextInt();
day = date / 1000000;
month = (date / 10000) - (day * 100);
year = date - (date / 10000) * (10000);
switch(day)
{
case 1: case 21: case 31:
suffix = "st";
break;
case 2: case 22:
suffix = "nd";
break;
case 3: case 23:
suffix = "rd";
break;
case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
suffix = "th";
break;
default:
System.out.println("Error, Please enter a valid day (i.e. DD) that is between 01 - 31");
break;
}
switch(month)
{
case 4: case 6: case 9: case 11:
if ((day < 1) || (day > 30))
{
System.out.println("Error this day does not exist");
}
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if ((day < 1) || (day > 31))
{
System.out.println("Error this day does not exist");
}
break;
case 2:
if ((day < 1) || (day > 28))
{
if ((day != 29))
{
System.out.println("Error this day does not exist");
}
else if ((year % 4 != 0) || ((year % 400 != 0) && (year % 100 == 0)))
{
System.out.println("Error this day does not exist");
}
//If it isn't a leap year, febuary cannot have 29 days
}
break;
default:
System.out.println("Error this day does not exist");
}
switch(month)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "Febuary";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
default:
System.out.println("Error, Please make sure the month (i.e. MM) is between 01 and 12");
break;
}
if ((day == 29) && (month == 2))
{
System.out.println("It is the 29th day of Febuary in " + year + ".");
}
else
{
System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
}
}
{
System.out.println(“请输入要拆分的日期”);
System.out.println(“请确保其格式为DDMMYYYY”);
日期=sc.nextInt();
天=日期/1000000;
月份=(日期/10000)-(日期*100);
年份=日期-(日期/10000)*(10000);
开关(日)
{
案例1:案例21:案例31:
后缀=“st”;
打破
案例2:案例22:
后缀=“nd”;
打破
案例3:案例23:
后缀=“rd”;
打破
案例四:案例五:案例六:案例七:案例八:案例九:案例十:案例十一:案例十二:案例十三:案例十四:案例十五:案例十六:案例十七:案例十八:案例十九:案例二十:案例二十四:案例二十五:案例二十六:案例二十七:案例二十八:案例二十九:案例三十:
后缀=“th”;
打破
违约:
System.out.println(“错误,请输入介于01-31之间的有效日期(即DD”);
打破
}
开关(月)
{
案例4:案例6:案例9:案例11:
如果((第1天)| |(第30天))
{
System.out.println(“当天不存在错误”);
}
打破
案例1:案例3:案例5:案例7:案例8:案例10:案例12:
如果((第1天)| |(第31天))
{
System.out.println(“当天不存在错误”);
}
打破
案例2:
如果((第1天)| |(第28天))
{
如果((天!=29))
{
System.out.println(“当天不存在错误”);
}
如果((年份%4!=0)| |((年份%400!=0)&&(年份%100==0)),则为else
{
System.out.println(“当天不存在错误”);
}
//如果不是闰年,二月不能有29天
}
打破
违约:
System.out.println(“当天不存在错误”);
}
开关(月)
{
案例1:
monthName=“一月”;
打破
案例2:
monthName=“二月”;
打破
案例3:
monthName=“三月”;
打破
案例4:
monthName=“四月”;
打破
案例5:
monthName=“May”;
打破
案例6:
monthName=“June”;
打破
案例7:
monthName=“七月”;
打破
案例8:
monthName=“八月”;
打破
案例9:
monthName=“九月”;
打破
案例10:
monthName=“十月”;
打破
案例11:
monthName=“11月”;
打破
案例12:
monthName=“十二月”;
违约:
System.out.println(“错误,请确保月份(即MM)介于01和12之间”);
打破
}
如果((日=29)和&(月=2))
{
System.out.println(“这是“+年+”中的2月29日”);
}
其他的
{
System.out.println(“它是“+年+”中“+月+”的“+天+后缀+”天”);
}
}
所以,基本上,当我运行这个程序并输入70702020
值时,我将得到以下所有输出:
错误,请输入介于01-31之间的有效日期(即DD)
今天的错误不存在
错误,请确保月份(即MM)介于01和12之间
然后它将输出最终输出(这仅在用户输入有效日期的情况下使用)我建议查看注释,然后自己执行代码。 我补充一些意见
public class TestFormatDate{
public static void main(String []args){
System.out.println("Please enter the date you would like to split");
System.out.println("Please make sure it is in the format DDMMYYYY");
//your input:70702020
int date = 70702020;
//70
int day = date / 1000000;
//70
int month = (date / 10000) - (day * 100);
//2020
int year = date - (date / 10000) * (10000);
String suffix = null;
String monthName = null;
switch(day)
{
case 1: case 21: case 31:
suffix = "st";
break;
case 2: case 22:
suffix = "nd";
break;
case 3: case 23:
suffix = "rd";
break;
case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
suffix = "th";
break;
// day is 70, so the default is executed.
default:
System.out.println("Error, Please enter a valid day (i.e. DD) that is between 01 - 31");
break;
}
switch(month)
{
case 4: case 6: case 9: case 11:
if ((day < 1) || (day > 30))
{
System.out.println("Error this day does not exist");
}
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if ((day < 1) || (day > 31))
{
System.out.println("Error this day does not exist");
}
break;
case 2:
if ((day < 1) || (day > 28))
{
if ((day != 29))
{
System.out.println("Error this day does not exist");
}
else if ((year % 4 != 0) || ((year % 400 != 0) && (year % 100 == 0)))
{
System.out.println("Error this day does not exist");
}
//If it isn't a leap year, febuary cannot have 29 days
}
break;
// month is 70, so the default is executed.
default:
System.out.println("Error this day does not exist");
}
switch(month)
{
case 1:
monthName = "January";
break;
case 2:
monthName = "Febuary";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
// month is 70, so the default is executed.
default:
System.out.println("Error, Please make sure the month (i.e. MM) is between 01 and 12");
break;
}
if ((day == 29) && (month == 2))
{
System.out.println("It is the 29th day of Febuary in " + year + ".");
}
// day is 70, so the else is executed.
else
{
System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
}
}
}
公共类TestFormatDate{
公共静态void main(字符串[]args){
System.out.println(“请输入要拆分的日期”);
System.out.println(“请确保其格式为DDMMYYYY”);
//您的输入:70702020
国际日期=70702020;
//70
整数天=日期/1000000;
//70
整月=(日期/10000)-(日期*100);
//2020
int year=日期-(日期/10000)*(10000);
字符串后缀=null;
字符串monthName=null;
开关(日)
{
案例1:案例21:案例31:
后缀=“st”;
打破
案例2:案例22:
后缀=“nd”;
打破
案例3:案例23:
后缀=“rd”;
打破
案例四:案例五:案例六:案例七:案例八:案例九:案例十:案例十一:案例十二:案例十三:案例十四:案例十五:案例十六:案例十七:案例十八:案例十九:案例二十:案例二十四:案例二十五:案例二十六:案例二十七:案例二十八:案例二十九:案例三十:
后缀=“th”;
打破
//日期为70,因此执行默认值。
违约:
System.out.println(“错误,请输入介于01-31之间的有效日期(即DD”);
打破
}
开关(月)
{
案例4:案例6:案例9:案例11:
如果((第1天)| |(第30天))
{
System.out.println(“当天不存在错误”);
}
打破
案例1:案例3:案例5:案例7:案例8:案例10:案例12:
如果((第1天)| |(第31天))
{
try {
LocalDate ld =
LocalDate
.parse(
"70702020" ,
DateTimeFormatter.ofPattern( "ddMMuuuu" )
)
;
} catch ( DateTimeParseException e ) {
…
}