小编Pit*_*itu的帖子

C#无法将类型'string'隐式转换为'bool'错误

我是C#的新手,我正在使用微软的Visual Studio Express 2013 Windows桌面版本,我正在尝试进行一个测验,我问这个问题,用户必须回答它,这里是代码,我得到的错误是"无法将类型'string'隐式转换为'bool'",这发生在2 if语句中,我知道bool的值为true或false但是它是一个字符串,为什么它会给我这个错误?任何帮助应该被赞赏. PS:我只包含了我遇到问题的代码部分,这是主类中唯一的代码

下面是代码:

 Start:
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Question 1: Test? type yes or no: ");
        String answer1 = Console.ReadLine();

        if (answer1 = "yes") {
            Console.WriteLine();
            Console.WriteLine("Question 2: Test? type Yes or no");
        }
        else if (answer1 = "no")
        {
            Console.WriteLine();
            Console.WriteLine("Wrong, restarting program");
            goto Start;
        }
        else {
            Console.WriteLine();
            Console.WriteLine("Error");
            goto Start;
        }
Run Code Online (Sandbox Code Playgroud)

c# string console boolean

-1
推荐指数
1
解决办法
2万
查看次数

标签 统计

boolean ×1

c# ×1

console ×1

string ×1