首页 > 解决方案 > 无法使用布尔值获得特定输出

问题描述

我想要一个特定的输出,具体取决于布尔值是否设置为真。我似乎找不到解决方案,所以我只是在这里问。我不输出“真”或“假”,而是输出特定文本。

我尝试了什么:

public class Program
{
    bool isAlive = true;
    public static void Main()
    {
        if (isAlive == true){
            Console.WriteLine("Is True");
        }
    }
}

什么有效:

public class Program
{
    public static void Main()
    {
        bool isAlive = true;
        if (isAlive == true){
            Console.WriteLine("Is True");
        }
    }
}

The Error was that I tried to use the non-static variable in the static void. Thanks to everyone that helped. 

标签: c#boolean

解决方案


您可以使用以下代码

{boolean_variable} ?“布尔值为真”:“布尔值为假”


推荐阅读