dimanche 30 juillet 2023

Cannot implicitly convert type 'bool' to 'int' with switch statement

I'm very new to c#, and I'm trying to make a simple number-guessing game, basically the user enters their guess and if the guess is the higher than the number, the system will display the it's higher or lower.

this is the code:

using System.Diagnostics.Tracing;

Random random = new Random();   

int num = random.Next(0,101);

Console.WriteLine("This is a number guessing game between 1 - 100 ! ");
Console.WriteLine("Enter your guess : ");
int respond = Convert.ToInt32(Console.ReadLine());



switch (respond) {

    case respond > num: Console.WriteLine("");
        break;

    case respond < num: Console.WriteLine("");
        break;
    case respond == num  : Console.WriteLine("You won !");
        break;


}




Console.WriteLine("You won !");
Console.WriteLine("It took you " + tries + " tries");



Console.ReadKey();

the problem comes in this part, the switch :

switch (respond) {

    case respond > num: Console.WriteLine("");
        break;

    case respond < num: Console.WriteLine("");
        break;
    case respond == num  : Console.WriteLine("You won !");
        break;
}

I always get an error when trying to compare 'respond' and 'num', specifically, this error :

Cannot implicitly convert type 'bool' to 'int'

does anyone know how to fix it?

I tried changing the place and the variable respond, same with 'num' nothing seemed to work.




Aucun commentaire:

Enregistrer un commentaire