【发布时间】:2016-04-15 14:22:10
【问题描述】:
我制作了一个扫描仪,它要求输入 3 个整数:月/日/出生年 对于每个整数,我检查这是否是一个有效的条目(即月 0)。 如果三个主菜是有效的,我会做一个:
System.out.println(month + "-" + day + "-" + year);
完整代码如下:
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int intJour;
int intMois;
int intAnnee;
do {
System.out.println("Year of birth :");
intJour = sc.nextInt();
sc.nextLine();
} while (intJour < 0 || intJour > 31);
do {
System.out.println("Month of birth :");
intMois = sc.nextInt();
sc.nextLine();
} while (intMois < 0 || intMois > 12);
do {
System.out.println("Year of birth :");
intAnnee = sc.nextInt();
sc.nextLine();
} while (intAnnee < 0 || intAnnee > Calendar.getInstance().get(Calendar.YEAR));
System.out.println("Birth date: " + intJour + "-" + intMois + "-" + intAnnee);
我想从用户输入中确定一个日期,并检查该日期是否存在。 I.E 并不是每个月都有 31 个关于所涉及的年份。
【问题讨论】:
-
使用
if(condition)子句来放置所有条件。此外,这看起来像是一个家庭作业问题。 -
条件有效。例如,用户不能输入 13/32/2017。但有时满足条件:2015 年 2 月 31 日但日期不存在,因为它是 bisextile(它的一个例子)。
-
当
if条件不成立时,使用else采取行动。 -
我需要参考日历,这就是我努力做的事情。我的问题可能不够准确。
-
我编辑了因为我认为我的问题不清楚。不是每个条件的验证都是一个问题。我想从用户输入中确定一个日期,并检查该日期是否存在。 I.E 并非每个月都有 31 项涉及的年份。