验证用户输入的日期
Validating date from user input
我正在编写一个程序,我应该让用户输入 0 - 4000 年的日期。我应该查看日期是否有效,以及是否是闰年.我的代码有问题。
我在第 57 行得到了一个没有错误的 else。
我也不确定如何说日期是否有效。
IE:此日期有效,是闰年 - 或无效不是闰年...等...
我还是个初学者,所以我不想为我编写代码,但我想知道如何修复它!谢谢你。
import java.util.*;
public class LegalDate //file name
{
public static void main (String [] args)
{
Scanner kb = new Scanner (System.in); //new scanner
//name the variables
int month, day, year;
int daysinMonth;
boolean month1, year1, day1;
boolean validDate;
boolean leapYear;
//ask the user for input
//I asked the MM/DD/YYYY in seperate lines to help me visually with the program
System.out.println("Please enter the month, day, and year in interger form: " );
kb.nextInt();
//now I'm checking to see if the month and years are valid
if (month <1 || month >12)
{ month1 = true;}
if (year <0 || year >4000)
{year1= true;}
//I'm using a switch here instead of an if-else statement, which can also be used
switch (month) {
case 1:
case 3:
case 5: //months with 31 days
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6: //months with 30 days
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) //formula for leapyear
numDays = 29;
{
system.out.println("is a leap year");
}
else
numDays = 28;
{
system.out.println("is not a leap year");
}
break;
default:
System.out.println("Invalid month.");
break;
if (month1 == true)
if (day1 == true)
if (year1 == true)
System.out.println ("date is valid ");
else
if (month1 == false)
System.out.println ("date is invalid");
else
if (day1 == false)
System.out.println ("date is invalid");
else
if (year1 == false)
System.out.println ("date is invalid");
}}
}
在第 57 行,您打开了一个新代码块,但无法访问它。我相信你的意思是输入:
else{
numDays = 28;
system.out.println("is not a leap year");
}
作为小提示,您可以更改为:
if (month1 == true)
if (day1 == true)
if (year1 == true)
System.out.println ("date is valid ");
对此:
if (month1 && day1 && year1)
System.out.println ("date is valid ");
由于布尔比较运算符 return true 或 false,您可以判断条件只需要是布尔值。由于 month1
、day1
和 year1
都是布尔值,因此您无需将它们与任何内容进行比较。
条件的意思是,如果你不知道,如果 month1
和 day1
和 year1
都为真,那么打印 date is valid
你为什么不试试 Java 8 日期时间 API
它会验证日期并为您做更多事情
喜欢
try {
LocalDate date =LocalDate.of(2016, 12, 31);
if(date.isLeapYear())
System.out.println("Leap year");
else
System.out.println("Not leap year");
}
catch(DateTimeException e) {
System.out.println(e.getMessage());
}
您程序第 50 行的 if-else 语句语法不正确。
这是一个悬而未决的事情。将 if 和 else 语句的主体括在大括号内应该可以解决此问题。
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
{
numDays = 29;
system.out.println("is a leap year");
}
else
{
numDays = 28;
system.out.println("is not a leap year");
}
您可以利用IDE或注意编译器错误信息来解决此类错误。
您似乎没有将 'if' 和 'else' 语句之间的代码放在花括号中,这意味着该语句将仅适用于下一行。例如:
if (a)
b = true
c = true
else
d = true
读作
if (a) {
b = true
}
c = true
else {
d = true
}
希望您能明白编译器是如何理解这一点的,因为 'else' 语句必须直接出现在其关联的 'if' 块之后。
我建议添加一些方法来简化您的代码。例如:
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
还有,如果用boolean变量存储信息,最后可以整齐的打印出来。例如,您可以在代码顶部将变量 'isValid' 实例化为 true,如果计算出日期无效,则将其设置为 false,并在末尾使用 if 语句打印结果。
我知道您说过您不想为您编写它,但这是证明方法重要性的最简单方法。希望您能看到这比您的版本更具可读性吗?
import java.util.Scanner;
public class LegalDate {
static final int maxYear = 4000;
public static void main (String [] args) {
int month, day, year;
boolean leapYear, validDate = false;
Scanner kb = new Scanner (System.in);
System.out.println("Please enter the month, day, and year in interger form.");
System.out.print("Month: ");
month = kb.nextInt();
System.out.print("Day: ");
day = kb.nextInt();
System.out.print("Year: ");
year = kb.nextInt();
leapYear = isLeapYear(year);
validDate = isValidDate(month, day, year);
System.out.printf("%nThe date is %svalid and is %sa leap year.%n", validDate ? "" : "not ", leapYear ? "" : "not ");
kb.close();
}
public static int numDaysInMonth(int month, boolean isLeapYear) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear) {
return 29;
} else {
return 28;
}
default:
return 0;
}
}
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
public static boolean isValidDate(int month, int day, int year) {
return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear);
}
}
如果您有任何问题,我会尽力回答!
我正在编写一个程序,我应该让用户输入 0 - 4000 年的日期。我应该查看日期是否有效,以及是否是闰年.我的代码有问题。 我在第 57 行得到了一个没有错误的 else。 我也不确定如何说日期是否有效。 IE:此日期有效,是闰年 - 或无效不是闰年...等...
我还是个初学者,所以我不想为我编写代码,但我想知道如何修复它!谢谢你。
import java.util.*;
public class LegalDate //file name
{
public static void main (String [] args)
{
Scanner kb = new Scanner (System.in); //new scanner
//name the variables
int month, day, year;
int daysinMonth;
boolean month1, year1, day1;
boolean validDate;
boolean leapYear;
//ask the user for input
//I asked the MM/DD/YYYY in seperate lines to help me visually with the program
System.out.println("Please enter the month, day, and year in interger form: " );
kb.nextInt();
//now I'm checking to see if the month and years are valid
if (month <1 || month >12)
{ month1 = true;}
if (year <0 || year >4000)
{year1= true;}
//I'm using a switch here instead of an if-else statement, which can also be used
switch (month) {
case 1:
case 3:
case 5: //months with 31 days
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6: //months with 30 days
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) //formula for leapyear
numDays = 29;
{
system.out.println("is a leap year");
}
else
numDays = 28;
{
system.out.println("is not a leap year");
}
break;
default:
System.out.println("Invalid month.");
break;
if (month1 == true)
if (day1 == true)
if (year1 == true)
System.out.println ("date is valid ");
else
if (month1 == false)
System.out.println ("date is invalid");
else
if (day1 == false)
System.out.println ("date is invalid");
else
if (year1 == false)
System.out.println ("date is invalid");
}}
}
在第 57 行,您打开了一个新代码块,但无法访问它。我相信你的意思是输入:
else{
numDays = 28;
system.out.println("is not a leap year");
}
作为小提示,您可以更改为:
if (month1 == true)
if (day1 == true)
if (year1 == true)
System.out.println ("date is valid ");
对此:
if (month1 && day1 && year1)
System.out.println ("date is valid ");
由于布尔比较运算符 return true 或 false,您可以判断条件只需要是布尔值。由于 month1
、day1
和 year1
都是布尔值,因此您无需将它们与任何内容进行比较。
条件的意思是,如果你不知道,如果 month1
和 day1
和 year1
都为真,那么打印 date is valid
你为什么不试试 Java 8 日期时间 API
它会验证日期并为您做更多事情
喜欢
try {
LocalDate date =LocalDate.of(2016, 12, 31);
if(date.isLeapYear())
System.out.println("Leap year");
else
System.out.println("Not leap year");
}
catch(DateTimeException e) {
System.out.println(e.getMessage());
}
您程序第 50 行的 if-else 语句语法不正确。 这是一个悬而未决的事情。将 if 和 else 语句的主体括在大括号内应该可以解决此问题。
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
{
numDays = 29;
system.out.println("is a leap year");
}
else
{
numDays = 28;
system.out.println("is not a leap year");
}
您可以利用IDE或注意编译器错误信息来解决此类错误。
您似乎没有将 'if' 和 'else' 语句之间的代码放在花括号中,这意味着该语句将仅适用于下一行。例如:
if (a)
b = true
c = true
else
d = true
读作
if (a) {
b = true
}
c = true
else {
d = true
}
希望您能明白编译器是如何理解这一点的,因为 'else' 语句必须直接出现在其关联的 'if' 块之后。
我建议添加一些方法来简化您的代码。例如:
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
还有,如果用boolean变量存储信息,最后可以整齐的打印出来。例如,您可以在代码顶部将变量 'isValid' 实例化为 true,如果计算出日期无效,则将其设置为 false,并在末尾使用 if 语句打印结果。
我知道您说过您不想为您编写它,但这是证明方法重要性的最简单方法。希望您能看到这比您的版本更具可读性吗?
import java.util.Scanner;
public class LegalDate {
static final int maxYear = 4000;
public static void main (String [] args) {
int month, day, year;
boolean leapYear, validDate = false;
Scanner kb = new Scanner (System.in);
System.out.println("Please enter the month, day, and year in interger form.");
System.out.print("Month: ");
month = kb.nextInt();
System.out.print("Day: ");
day = kb.nextInt();
System.out.print("Year: ");
year = kb.nextInt();
leapYear = isLeapYear(year);
validDate = isValidDate(month, day, year);
System.out.printf("%nThe date is %svalid and is %sa leap year.%n", validDate ? "" : "not ", leapYear ? "" : "not ");
kb.close();
}
public static int numDaysInMonth(int month, boolean isLeapYear) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear) {
return 29;
} else {
return 28;
}
default:
return 0;
}
}
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
public static boolean isValidDate(int month, int day, int year) {
return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear);
}
}
如果您有任何问题,我会尽力回答!