Java 从用户输入验证日期
我正在写一个程序,我应该让用户输入一个0-4000年的日期。我要看看这个日期是否有效,是否是闰年。我的代码有问题。 我在第57行收到了一个无误的else。 我也不知道该怎么说日期是否有效。 IE:这个日期是有效的,是闰年-或者是无效的,不是闰年…等等 我还是一个初学者,所以我不想为我写的代码,但我想知道如何修复它!多谢各位Java 从用户输入验证日期,java,validation,date,switch-statement,Java,Validation,Date,Switch Statement,我正在写一个程序,我应该让用户输入一个0-4000年的日期。我要看看这个日期是否有效,是否是闰年。我的代码有问题。 我在第57行收到了一个无误的else。 我也不知道该怎么说日期是否有效。 IE:这个日期是有效的,是闰年-或者是无效的,不是闰年…等等 我还是一个初学者,所以我不想为我写的代码,但我想知道如何修复它!多谢各位 import java.util.*; public class LegalDate //file name { public static void
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");
}}
}
import java.util.*;
公共类LegalDate//文件名
{
公共静态void main(字符串[]args)
{
Scanner kb=新扫描仪(System.in);//新扫描仪
//命名变量
int月、日、年;
int daysinMonth;
布尔值month1,year1,day1;
布尔有效值;
一年一次;
//请求用户输入
//我要求MM/DD/YYYY在单独的行中帮助我直观地使用该程序
System.out.println(“请以整数形式输入月、日和年:”);
kb.nextInt();
//现在我正在检查月份和年份是否有效
如果(第12个月)
{month1=true;}
如果(4000年)
{year1=true;}
//我在这里使用的是switch而不是if-else语句,它也可以被使用
开关(月){
案例1:
案例3:
案例5://个月加31天
案例7:
案例8:
案例10:
案例12:
numDays=31;
打破
案例4:
案例6://个月加30天
案例9:
案例11:
numDays=30;
打破
案例2:
如果((第%4年==0)和&!(第%100年==0))| |(第%400年==0))//年的公式
numDays=29;
{
system.out.println(“是闰年”);
}
其他的
numDays=28;
{
system.out.println(“不是闰年”);
}
打破
违约:
System.out.println(“无效月份”);
打破
如果(month1==真)
如果(第1天==真)
如果(第1年==真)
System.out.println(“日期有效”);
其他的
如果(month1==false)
System.out.println(“日期无效”);
其他的
如果(第1天==假)
System.out.println(“日期无效”);
其他的
如果(第1年==假)
System.out.println(“日期无效”);
}}
}
在第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 ");
由于布尔比较运算符返回true或false,因此可以看出条件只需要是布尔值。由于month1
、day1
和year1
都是布尔值,因此不需要将它们与任何值进行比较
如果您不知道,条件的意思是如果
month1
和day1
和year1
都是真的,那么打印日期是有效的
为什么不试试Java 8 date timeAPI
它可以验证日期,并为您做更多事情
像
程序第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
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
读作
if (a) {
b = true
}
c = true
else {
d = true
}
希望您能看到编译器是如何不理解这一点的,因为“else”语句必须直接出现在其关联的“if”块之后
我建议添加一些方法来简化代码。例如:
if (a)
b = true
c = true
else
d = true
public static boolean isLeapYear(int year) {
return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}
此外,如果使用布尔变量存储信息,则可以在末尾整齐地打印所有信息。例如,可以在代码顶部将变量“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);
}
}
import java.util.Scanner;
公共级议会{
静态最终整数最大年=4000;
公共静态void main(字符串[]args){
int月、日、年;
布尔值leapYear,validDate=false;
扫描仪kb=新扫描仪(System.in);
System.out.println(“请以整数形式输入月份、日期和年份”);
系统输出打印(“月:”);
月份=kb.nextInt();
系统输出打印(“日期:”;
day=kb.nextInt();
系统输出打印(“年:”);
年份=kb.nextInt();
leapYear=isLeapYear(年);
validDate=isValidDate(月、日、年);
System.out.printf(“%n日期为%s有效且为%s闰年。%n”,有效期?”:“否”,闰年?”:“否”);
kb.close();
}
公共静态整数numDaysInMonth(整数月,布尔值isLeapYear){
开关(月){