简体   繁体   中英

C# Cannot implicitly convert type 'string' to 'bool' error

I'm new to C# I'm using microsoft Visual Studio Express 2013 Windows Desktop edition and I was trying to make a quiz in which I ask the question and the user has to answer it so, here's the code and the error i get is "Cannot implicitly convert type 'string' to 'bool'" and this happens on the 2 if statements, I understand that a bool has either the value true or false however it's a string so why is it giving me this error? Any help should be appreciated. PS: I only included the part of the code in which i'm having the problem and this is the only code in the main class

Heres the code:

 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;
        }

in all of your if statements

if (answer1 = "yes")

should be

if (answer1 == "yes")

in c#, = is to assign a value, == is for comparison. Change it in all of your if statements and youll be fine

Please have a look at this line:

if (answer1 = "yes") {

This will assign "yes" to answer1 first and then it's like

if(answer1) { // answer1 = "yes"

So now this will try to convert answer1 which is a string into a boolean, which the if statement requires. This does not work and throws the exception.

You'll have to do a comparison like this:

if(answer1 == "yes") {

or you could use equals like this:

if("yes".Equals(answer1)) {

and then do the same for the else if.

The immediate reason is that = assigns, not compares values as == does. So you can do

   if (answer1 == "yes") {
     ...
   }

However I'd prefer

  if (String.Equals(answer1, "yes", StringComparison.OrdinalIgnoreCase)) {
    ...
  }

in case user chooses "Yes" or "YES" etc. as the answer

this = is the assignment operator in C#
this == is a comparison operator in C#

for a full list of the operators in C# check this out. As an asside I would generally recommend againt using goto statements

All that being said your code should look something like this.

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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