Pit*_*itu -1 c# string console boolean
我是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)
在所有的if语句中
if (answer1 = "yes")
Run Code Online (Sandbox Code Playgroud)
应该
if (answer1 == "yes")
Run Code Online (Sandbox Code Playgroud)
在c#中,=
是指定一个值,==
用于比较.在所有的if语句中更改它,你会没事的