gpt4 book ai didi

Java 有效日期和 JOptionPane

转载 作者:行者123 更新时间:2023-11-29 05:30:04 25 4
gpt4 key购买 nike

Noob 在这里,请耐心等待。我正在开发一个 Java 程序,该程序从 JoptionPane 获取用户输入,并使用一组 if-else 语句来验证日期,同时考虑闰年。我在运行时遇到的问题似乎是它验证了不可能的日期。例如,2/29/2014,它说这是一个有效日期,但实际上它不是。对此问题的见解将不胜感激。

 import java.util.Arrays;
import javax.swing.JOptionPane;

public class vaildDate {

public static void main(String[] args) {
// TODO Auto-generated method stub


int month, day, year;

String userInput = JOptionPane.showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
String [] date = userInput.split("/");
String mm = date[0];
String dd = date [1];
String yyyy = date [2];
month = Integer.parseInt(mm);
day = Integer.parseInt(dd);
year = Integer.parseInt(yyyy);
System.out.println(mm+dd+yyyy);



boolean validLeapYear;
boolean validDate;
if(month>=1 && month <=12&& day>=1 && day<=31)
{

if(month == 4 || month == 9 || month == 6 || month == 11 && day <= 30)
{
validDate = true;
}

if(( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 11 || month == 12) && (day <= 31))
{
validDate = true;
}
else{
validDate =false;

}

if((year % 400 == 0 ) || (year % 4 == 0) && (year % 100 != 0))
{

validLeapYear = true;

}
if ((month == 2 && day == 29) && (validLeapYear = false ))
{
validDate = false;
}
if((month == 2 && day <= 29) && (validLeapYear = true ))
{
validDate = true;
}


if(validDate = true)
{
JOptionPane.showMessageDialog(null, month +"/"+day+"/"+year+" is a valid date");
}

else if(validDate = false) {
JOptionPane.showMessageDialog(null,"Your date is invalid");
}
}
}
}

最佳答案

您分配的不是等同于 booleans 的值,而是返回分配结果。

例如:

if (validDate = true)

上述语句将返回 true。相反,在所有 boolean 检查中,只需按原样验证 boolean 值。

if (validDate)

要消除编译器错误,您可以将 2 个 boolean 变量初始化为 false

您更改的代码看起来像这样:

 public static void main(String[] args) {
// TODO Auto-generated method stub
int month, day, year;

String userInput = JOptionPane
.showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
String[] date = userInput.split("/");
String mm = date[0];
String dd = date[1];
String yyyy = date[2];
month = Integer.parseInt(mm);
day = Integer.parseInt(dd);
year = Integer.parseInt(yyyy);
System.out.println(mm + dd + yyyy);

boolean validLeapYear = false;
boolean validDate = false;

if (month >= 1 && month <= 12 && day >= 1 && day <= 31) {

if (month == 4 || month == 9 || month == 6 || month == 11
&& day <= 30) {

validDate = true;
}

if ((month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 11 || month == 12)
&& (day <= 31)) {
validDate = true;

} else {

validDate = false;

}

if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)) {

validLeapYear = true;

}
if ((month == 2 && day == 29) && (!validLeapYear)) {

validDate = false;
}
if ((month == 2 && day <= 29) && (validLeapYear)) {

validDate = true;
}

if (validDate) {

JOptionPane.showMessageDialog(null, month + "/" + day + "/"
+ year + " is a valid date");
}

else if (!validDate) {
JOptionPane.showMessageDialog(null, "Your date is invalid");
}
}
}

关于Java 有效日期和 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com