简体   繁体   English

为什么我的if语句总是评估为true?

[英]Why does my if statement always evaluate to true?

I need to go through the months of the year and find out if the last day of the month is 28, 29, 30 or 31. My problem is that the first if statement always evaluates to true: 我需要检查一年中的几个月,然后找出该月的最后一天是28、29、30还是31。我的问题是,第一个if语句的总值为true:

MOIS_I = 31
if (mois == "Janvier" || "Mars" || "Mai" || "Juillet" || "Août" || "Octobre" || "Décembre" || "1" || "3" || "5" || "7" || "8" || "10" || "12" || "01" || "03" || "05" || "07" || "08") {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
}

Also, it seems like it is necessary to do if (mois == "Janver" || mois == "Février" || ... ) and so on, but I wanted to know if there was a better way to do it. 另外,似乎有必要执行以下操作if (mois == "Janver" || mois == "Février" || ... ) ,依此类推,但是我想知道是否有更好的方法。

Here is the full code: 这是完整的代码:

    var mois, annee, test4, test100, test400;
    const MOIS_P = 30;
    const MOIS_I = 31;
    const FEV_NORM = 28; 
    const FEV_BISSEX = 29;
    const TEST_4 = 4;
    const TEST_100 = 100;
    const TEST_400 = 400;

    mois = window.prompt("Entrez un mois de l'année", "");
    annee = window.prompt("Entrez l'année de ce mois", "");
    /* MOIS IMPAIRS */
    if (mois == "Janvier" || "Mars" || "Mai" || "Juillet" || "Août" || "Octobre" || "Décembre" || "1" || "3" || "5" || "7" || "8" || "10" || "12" || "01" || "03" || "05" || "07" || "08") {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
    /* MOIS PAIRS */
    } else if (mois == "Février" || "Avril" || "Juin" || "Septembre" || "Novembre" || "2" || "4" || "6" || "9" || "11" || "02" || "04" || "06" || "09") { 
        if (mois == "Février") {
            test4 = parseInt(annee) % TEST_4;
            test100 = parseInt(annee) % TEST_100;
            test400 = parseInt(annee) % TEST_400;
            if (test4 == 0) {
                if (test100 != 0) {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_BISSEX + " jours ");
                } else {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_NORM + " jours ");
                }
            } else if (test400 == 0) {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_BISSEX + " jours ");
            } else {
                window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_NORM + " jours ");
            }
        } else {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_P + " jours ");
        }           
    } else {
        window.alert("Apocalypse!");
    }

TEST_4, TEST_100, TEST_400 are to test if the year is a leap year (which means february has 29 days instead of 28). TEST_4,TEST_100,TEST_400将测试年份是否为a年(这意味着2月为29天而不是28天)。

Thank you! 谢谢!

do like this (the simple way) 做到这一点(简单的方法)

var last_day=new Date(year,month,-1).getDate()

this will give the last day of month 这将给出一个月的最后一天

In terms of if statement, yes, you always need to add mois == part, otherwise you're checking boolean value of string "Fevrier" instead of comparing it to mois variable value. if语句而言,是的,您始终需要添加mois == part,否则,您要检查字符串“ Fevrier”的布尔值,而不是将其与mois变量值进行比较。

In terms of the general result you're trying to achieve, there are probably a lot of easier ways, available for you in the standard library. 就您要获得的总体结果而言,标准库中可能有许多更简单的方法可供您使用。 If this is JavaScript, see eg this article . 如果这是JavaScript,请参见例如本文

Here is how I would go about it: 这是我的处理方法:

  1. Get month and convert it to a number. 获取月份并将其转换为数字。 You'll probably want to use a hash to do this. 您可能需要使用哈希来执行此操作。
  2. Get year and convert it to a number, using parseInt() as you have done. 完成操作,使用parseInt()获取year并将其转换为数字。
  3. If month % 2 == 1 , then its an odd month: 如果month % 2 == 1 ,则其为奇数月:
    • If year % 400 == 0 || year % 100 == 0 || year % 4 == 0 如果year % 400 == 0 || year % 100 == 0 || year % 4 == 0 year % 400 == 0 || year % 100 == 0 || year % 4 == 0 year % 400 == 0 || year % 100 == 0 || year % 4 == 0 : handle leap years appropriately. year % 400 == 0 || year % 100 == 0 || year % 4 == 0 :适当地处理leap年。
    • Else: return 30. 否则:返回30。
  4. Else: its an even months, return 31. 否则:偶数月,返回31。

Here's a shortening suggestion for your if() conditions: the RegExp Object (although this will not answer your question). 以下是针对if()条件的简短建议: RegExp对象 (尽管这不会回答您的问题)。

/^(Janvier|Mars|Mai|Juillet|Août|(Octo|Décem)bre|0?[13578]|1[02])$/.test(mois))
// append i after the last forward slash if you want it not to respect case

is the same with 与...相同

if (
mois == "Janvier" || mois == "Mars" || mois == "Mai" || mois == "Juillet" ||
mois == "Août" || mois == "Octobre" || mois == "Décembre" || mois == "1" ||
mois == "3" || mois == "5" || mois == "7" || mois == "8" || mois == "10" ||
mois == "12" || mois == "01" || mois == "03" || mois == "05" || mois == "07" ||
mois == "08"
)

and

if (/^(Février|Avril|Juin|(Sept|Nov)embre|0?[2469]|11)$/.test(mois))
// same thing about the case-insensitive (i) flag here...

is the same with 与...相同

if (
mois == "Février" || mois == "Avril" || mois == "Juin" || mois == "Septembre" ||
mois == "Novembre" || mois == "2" || mois == "4" || mois == "6" || mois == "9" ||
mois == "11" || mois == "02" || mois == "04" || mois == "06" || mois == "09"
)

This seems to work without tricky date math: 这似乎没有棘手的日期数学就可以工作:

var SomeDate = new Date(2012, 10);  // November, 2012

SomeDate.setDate(SomeDate.getDate() - 1);  // go back one day

var DaysInMonth = SomeDate.getDate();

Here's a better way to check against a whole bunch of strings. 这是检查一堆字符串的更好方法。 You put all the allowed names in an object (often called a map) and then you can check to see if the name is in the map with one line of code: 您将所有允许的名称放入一个对象(通常称为地图)中,然后可以用一行代码检查名称是否在地图中:

var names = {
    "Janvier": true, "Mars": true, "Mai": true, "Juillet": true, 
    "Août": true, "Octobre": true, "Décembre": true, 
    "1": true, "3": true, "5": true, "7": true, "8": true, "10": true, "12": true, 
    "01": true, "03": true, "05": true, "07": true, "08": true
};

if (names[mois] === true) {
     window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM