用户输入 3 个整数(MM、dd、yyyy)时的日期验证 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26005814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date Validation Java when user inputs 3 integers (MM, dd, yyyy)
提问by Bob
Write a program that receives 3 integer inputs, which represent a month, a date, and a year (for example, input of 9 23 2013 represents September 23, 2013). The program outputs whether or not the input represents a valid date. Keep in mind that April, June, September, and November have 30 days, February has either 28 or 29 days (depending on whether it is a leap year), and the other months have 31 days. Leap years are divisible by 4, with the exception of years at the end of each century (such as 2000), which are not leap years. You may assume that the user inputs only positive integers.
编写一个程序,接收 3 个整数输入,分别代表月份、日期和年份(例如,输入 9 23 2013 代表 2013 年 9 月 23 日)。程序输出输入是否代表有效日期。请记住,4 月、6 月、9 月和 11 月有 30 天,2 月有 28 天或 29 天(取决于是否为闰年),其他月份有 31 天。闰年可以被 4 整除,但每个世纪末(例如 2000 年)的年份不是闰年。您可以假设用户只输入正整数。
This is the code I have so far, I've been using if/else statements which I know are not efficient but since I am a beginner at java it did not matter much to me. The program works fine for certain checks but when it doesn't it skips most of my code and doesn't return anything. tried messing with the brackets but no luck. Any help?
这是我到目前为止的代码,我一直在使用我知道效率不高的 if/else 语句,但由于我是 Java 的初学者,所以对我来说并不重要。该程序对某些检查工作正常,但是当它不工作时,它会跳过我的大部分代码并且不返回任何内容。尝试弄乱括号但没有运气。有什么帮助吗?
package calendar;
import java.util.Scanner;
public class Calendar
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int month;
int day;
int year;
System.out.println("Enter your month:");
month = sc.nextInt();
System.out.println("Enter your day:");
day = sc.nextInt();
System.out.println("Enter your year:");
year = sc.nextInt();
if(month > 12)
{
return;
}
else if (year % 4 == 0)
{
return;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day == 31)
{
return;
}
else if (day != 31)
{
System.out.println("False.");
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day == 30)
{
return;
}
else if (day != 30)
{
System.out.println("False.");
}
}
else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day == 29)
{
return;
}
else if (day != 29)
{
System.out.println("False.");
}
else if (year % 4 != 0)
{
if (day == 28)
{
return;
}
else if (day != 28)
{
System.out.println("False.");
}
}
}
}
else // Everything checks out
{
System.out.println("True.");
}
}
}
}
回答by arcy
You don't tell us what data causes the "return nothing" behavior you describe, but it looks to me like many of the times that don't pass your tests do a return statement out of main() without printing anything. For example: month > 12
, you return. year % 4 == 0
, you return.
您没有告诉我们是什么数据导致了您所描述的“无返回”行为,但在我看来,很多时候没有通过测试的人会在 main() 之外执行 return 语句而不打印任何内容。例如:month > 12
,您返回。 year % 4 == 0
,你回来。
ANYWHERE you put "return" out of main, put a println to indicate what you found.
任何你把“返回”放在 main 之外的地方,放一个 println 来表明你发现了什么。
Or learn to use a debugger.
或者学习使用调试器。
回答by Bryan Linton
Don't use return inside the if statements that's your problem. Make a boolean in the beginning called like isTrueDate, make it true, and if one of the conditions occur to make it not a true date, set it to false.
不要在 if 语句中使用 return 是您的问题。在开头创建一个名为 isTrueDate 的布尔值,使其为真,如果出现使它不是真日期的条件之一,则将其设置为假。
So something like ...
所以像...
year = sc.nextInt();
boolean isTrueDate = true;
if(month > 12)
{
isTrueDate = false;
}
and do this for everytime the date is not a real date.
并在每次日期不是真实日期时执行此操作。
At the end put
最后放
if (isTrueDate) {
System.out.println("True.");
}
else {
System.out.println("False.");
}
回答by Kumar
Use JodaTime API; and instantiate the DateTime class object by the given input. If the input date is not correct/invalid; it will throw exception. You can use TimeZone if working in different timezone.
使用 JodaTime API;并通过给定的输入实例化 DateTime 类对象。如果输入的日期不正确/无效;它会抛出异常。如果在不同的时区工作,您可以使用 TimeZone。
回答by Amol Giri
package calendar;
import java.util.Scanner;
class Calendar
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int day;
int month;
int year;
System.out.println("Enter Day: ");
day = sc.nextInt();
System.out.println("Enter Month");
month = sc.nextInt();
System.out.println("Enter Year");
year = sc.nextInt();
System.out.print(+day);
System.out.print("/");
System.out.print(+month);
System.out.print("/");
System.out.print(+year);
boolean isTrueDate = true;
if(month > 12)
{
isTrueDate = false;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day <= 31)
{
isTrueDate = true;
}
else if (day >= 31)
{
isTrueDate = false;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day <= 30)
{
isTrueDate = true;
}
else if (day >= 30)
{
isTrueDate = false;
}
}
else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day <= 29)
{
isTrueDate = true;
}
else if (day >= 29)
{
isTrueDate = false;
}
}
else if (year % 4 != 0)
{
if (day <= 28)
{
isTrueDate = true;
}
else if (day >= 28)
{
isTrueDate = false;
}
}
}
if(isTrueDate)
{
System.out.println("Valid");
}
if(!isTrueDate)
{
System.out.println("Invalid");
}
}
}