简体   繁体   English

C#新手:如果语句&&与int

[英]c# newbie: if statement && with int

i'm crossing a problem in the code i made for the second Euler problem (sum of the even fibonacciresults until 4million. 我正在为第二个Euler问题(偶数fibonacciresults之和直到400万为止)编写的代码中遇到一个问题。

i'm using an if statement with integers (also tried long, ...) and it sais the '&&' can not be used with int. 我正在使用带整数的if语句(也尝试了很久,...),它说'&&'不能与int一起使用。 how can i fix this or did i make another mistake ? 我该如何解决这个问题或者我犯了另一个错误?

tnx in advance 提前发送

        int result = 3;
        int resultMinEen = 1;
        int resultMinTwee = 2;        

        for (int i = 1; i <= 4000000; i++)
        {
            if ((i % 2) == 0 && i = resultMinEen + resultMinTwee)
            {
                result += i;
                resultMinTwee = resultMinEen;
                resultMinEen = result;
            }
        }

(i = resultMinEen + resultMinTwee) is what is going to return an integer. (i = resultMinEen + resultMinTwee)将返回整数。 It is setting the value of i, which is the loop variable. 它正在设置i的值,这是循环变量。 If this is what you are intending to do then it is very bad practice and you should set a second, temporary variable inside the body of the if test and use that. 如果这是您要执行的操作,那么这将是非常不好的做法,您应该在if测试的主体内设置第二个临时变量,然后使用它。 If you are attempting to test that i is equal to resultMinEen + resultMinTwee, then make it == (comparison operator) instead of = (assignment operator). 如果您要测试i是否等于resultMinEen + resultMinTwee,则将其设为==(比较运算符),而不是=(赋值运算符)。

there is difference between = and == :) =和==之间有区别:)

 int result = 3;
    int resultMinEen = 1;
    int resultMinTwee = 2;



    for (int i = 1; i <= 4000000; i++)
    {
        if (((i % 2) == 0) && (i == resultMinEen + resultMinTwee))
        {
            result += i;
            resultMinTwee = resultMinEen;
            resultMinEen = result;
        }
    }

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

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