Home > Mobile >  the conditions in a loop are not working properly
the conditions in a loop are not working properly

Time:02-19

In this condition the if statement and the else if statement are not working properly.I am trying to build a hangman game, the problem is that when the guessed letter is correct and matches a letter in the word it increment the countToLoose and print countToLoose and at the same time it prints "That is a correct guess".

Can someone please fix the title of the question to proper title because I can't think of other one.

public static string GuessTheLetter(string word)
{
    //List<string> _guessed = new List<string>();
    char[] charctersWord = word.ToLower().ToCharArray();
    int countToLoose = 0;
    
    while (countToLoose != 5)
    {
        Console.WriteLine("Guess a letter");
        string theGuessedLetter = Console.ReadLine();
        theGuessedLetter.ToLower();
    
        //_guessed.Add(theGuessedLetter);
        
        for (int i = 0; i < charctersWord.Length; i  )
        {
            if (!theGuessedLetter.ToLower().Equals(charctersWord[i])) 
            {
                countToLoose   ;
                Console.WriteLine(countToLoose);
                break;
            }
            else if(String.Equals(theGuessedLetter.ToLower(), charctersWord) == true)
            {
                
                Console.WriteLine("That is a correct guess");
                
            }
        }
    }
    
    return "";
}

CodePudding user response:

This is because you are comparing theGuessedLetter with each character from the array charctersWord, so although the word contains your guessed char, countToLoose gets incremented by charctersWord's length - 1.

Also, theGuessedLetter should be a char instead of string:

char theGuessedLetter = Console.ReadLine()[0];

instead of

string theGuessedLetter = Console.ReadLine();

And you could use String.Contains instead of the for loop. I also introduce a new variable, userWon, to get to know if the user guesses the letter:

bool userWon = false
while (countToLoose != 5 and !userWon)
{
        Console.WriteLine("Guess a letter");
        string theGuessedLetter = Console.ReadLine();
        if (charctersWord.Contains(theGuessedLetter.ToLower()))
        {
             userWon = true;
        }
        countToLoose  ;
}

And, after, you can check if the user guessed the letter to show the message:

if (userWon)
{
     Console.WriteLine("That is a correct guess");
}

CodePudding user response:

try this it will work fine. you are not doing incorrect way.

public static string GuessTheLetter(string word)
{
    //List<string> _guessed = new List<string>();
    char[] charctersWord = word.ToLower().ToCharArray();
    int countToLoose = 0;
    
    while (countToLoose != 5)
    {
        Console.WriteLine("Guess a letter");
        string theGuessedLetter = Console.ReadLine();
        theGuessedLetter.ToLower();
    
        //_guessed.Add(theGuessedLetter);
        bool guess=false;
        for (int i = 0; i < charctersWord.Length; i  )
        {
            
             if(String.Equals(theGuessedLetter.ToLower(), charctersWord) == true)
            {
                guess = true;
                 break;
                
            }
        }
        if (guess) 
            {
                Console.WriteLine("That is a correct guess");
                countToLoose   ;
                Console.WriteLine(countToLoose);
                break;
            }
    }
    
    return "";
}

  •  Tags:  
  • c#
  • Related