C# C语言作业的评价 在C++中,可以将赋值作为条件进行评估,如下面所示:

C# C语言作业的评价 在C++中,可以将赋值作为条件进行评估,如下面所示:,c#,C#,我在C中尝试了同样的方法,但似乎不起作用 public class Solution { static int _x = 0; public static bool DoStuff() { if (_x > 10) { return false; } else { ++_x; return true; } } public static

我在C中尝试了同样的方法,但似乎不起作用

public class Solution {
    static int _x = 0;
    public static bool DoStuff() {


        if (_x > 10) {
            return false;
        } else {
            ++_x;
            return true;
        }
    }
    public static void Main(string[] args) {

        bool didStuff = false;
        while (didStuff = DoStuff()) {
            Console.WriteLine("You shouldn't see this more than 10 times...");
        }

        if( didStuff) {
            Console.WriteLine("Girl, you know it's true.");
        }

    }
}
输出:

You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
You shouldn't see this more than 10 times...
我希望看到,女孩,你知道这是真的,作为控制台窗口的最后一行


while的评估结果似乎是正确的,但作业有点不对劲。有人能解释为什么我没有看到控制台的最终输出吗?

只要didStuff为true,while循环就会继续运行,一旦循环结束,就意味着didStuff为false。这就是为什么if条件不满足,并且您期望的文本没有写入控制台。

如果didStuff为true,为什么您认为while循环会停止?好的,我刚刚删除了。这是漫长的一天。谢谢