这是我的代码,用于测试日期是否适用于2月份。用于测试leapYear是真还是假的顶部部分正常工作。如果我输入例如02/29/1963作为日期,则leapYear将= false。但由于某种原因,代码将继续,就像leapYear = true一样。最终结果将是:
else if (leapYear = true){
if ((day > 0) && (day < 30) && (month ==2)){
System.out.println("Your date is valid.)"`
它将输出“您的日期有效”。而它应该专门输出日期02/29/1963“你的日期无效,因为1963年2月只有28天。”);
我无法弄清楚为什么会这样做。谢谢你的时间。 已决议
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
leapYear = true;
} else {
leapYear = false;
}
if (leapYear = false) {
if ((day > 0) && (day < 29) && (month == 2)) {
System.out.println("Your date is valid.");
} else if (day > 29) {
System.out.println("Your date is invalid because there are only 28 days in February"
+ "for the year " + year + ".");
} else {
System.out.println("Your date is invalid because the day you entered does not exist.");
}
} else if (leapYear = true) {
if ((day > 0) && (day < 30) && (month == 2)) {
System.out.println("Your date is valid.");
} else if (day > 30) {
System.out.println("Your date is invalid because there are only 29 days in February for"
+ "the year " + year + ".");
} else {
System.out.println("Your date is invalid because the day you entered does not exist.");
}
}
答案 0 :(得分:2)
您的if
和else-if
条件不得为assignment操作而非comparison。
而不是if (leapYear = false){
尝试
if (leapYear == false){
或只是
if (!leapYear){
else if (leapYear = true){
相同。它将是
else if (leapYear == true){
或
else if (leapYear){
答案 1 :(得分:1)
您无法在var looplim = 1;
var infinite = 8;
var k = 1;
//not sure if I actually need to pass variables to function like this, although, fairly certain i am.
var generator = function(infinite,looplim,k){
//value of infinite will be slowly increasing, until squared value of k is greater, and subtracted.
while(infinite >= 8){
console.log("mobiasMobiasMobias");
//value of k, is retained, and incremented by 1.
k = k + 1;
//looplim increases, by + 8 basically, and is retained.
looplim = looplim + infinite;
//loopdestroyer....should be a local variable here, and does not really need to be retained since value k is being passed to it, which is retained.
loopdestroyer = k*k;
//new value of infinite is set and retained, the increasing value of looplim, stays above loopdestroyer, for 7 cycles, im pretty sure, unless i'm mathing wrong, looplim being 57, and loopdestroyer being 64, when the loop ends.
infinite = looplim - loopdestroyer;
}
};
generator(infinite,looplim,k);
语句中使用赋值运算符。你必须做一个平等检查:
if
答案 2 :(得分:1)
我意识到你不能将它用于你的家庭作业,但是对于其他人而言,Java在其java.time框架中包含了这个功能。
ZonedDateTime now = ZonedDateTime.now ( ZoneId.of ( "America/Montreal" ) );
Year year = Year.from ( now );
Boolean isLeap = year.isLeap ();
转储到控制台。
System.out.println ( "year: " + year + " isLeap: " + isLeap );
跑步时。
年:2015年是飞跃:错误
答案 3 :(得分:0)
试试这个:
if (leapYear == false) {
if ((day > 0) && (day < 29) && (month == 2)) {
System.out.println("Your date is valid.");
} else if (day >= 29) {
System.out.println("Your date is invalid because there are only 28 days in February"
+ "for the year " + year + ".");
} else {
System.out.println("Your date is invalid because the day you entered does not exist.");
}
} else if (leapYear == true) {
if ((day > 0) && (day < 30) && (month == 2)) {
System.out.println("Your date is valid.");
} else if (day > 30) {
System.out.println("Your date is invalid because there are only 29 days in February for"
+ "the year " + year + ".");
} else {
System.out.println("Your date is invalid because the day you entered does not exist.");
}
}