Boolean test in C#

Posted on 2012-09-08
Last Modified: 2012-09-08
I am trying to create a Boolean test for false  "Try Again" but I am getting an error on the else statement..... should I try it another way? Want to let the player know that they guessed wrong

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hangman
    class Program
        static void Main(string[] args)

            Console.Write("Welcome to Hangman! ");
            //string mysteryWord = "mysteryword";

            string[] wordBank = new string[10];
            wordBank[0] = "love";
            wordBank[1] = "abundance";
            wordBank[2] = "money";
            wordBank[3] = "code";
            wordBank[4] = "car";
            wordBank[5] = "hack";
            wordBank[6] = "lazy";
            wordBank[7] = "ambition";
            wordBank[8] = "excellence";
            wordBank[9] = "integrity";
            Random randGen = new Random();
            var idx = randGen.Next(0, 9);
            string mysteryWord = wordBank[idx];

            char[] guess = new char[mysteryWord.Length];

            for (int p = 0; p < mysteryWord.Length; p++)
                guess[p] = '*';

            while (true)
                Console.Write("Please enter a letter: ");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j = 0; j < mysteryWord.Length; j++)
                    if (playerGuess == mysteryWord[j])
                        guess[j] = playerGuess;


       // }
            //else  --- Getting Errors in this block
   // {
          //  Console.Write("Try Again);

Open in new window

Question by:December2000
    LVL 85

    Accepted Solution

    "Want to let the player know that they guessed wrong"

    Create a bool flag that gets toggled when a match is found.  Something like:
                    bool incorrect = true; // <-- assume they are wrong until proven otherwise
                    for (int j = 0; j < mysteryWord.Length; j++)
                        if (playerGuess == mysteryWord[j])
                            guess[j] = playerGuess;
                            incorrect = false;
                    if (incorrect)
                        Console.WriteLine("The letter '" + playerGuess + "' was not in the word.");

    Open in new window


    Author Closing Comment

    You are awesome idle!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    This article will show, step by step, how to integrate R code into a R Sweave document
    This video teaches viewers about errors in exception handling.
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now