C#DO while循环 - Hangman

时间:2017-06-03 19:24:06

标签: c#

我正在学习使用C#编写代码。我无法弄清楚为什么我的循环在10个条目进入我的Hangman程序后不会结束,尽管如果设置为较小的数字(例如6),程序可以工作。我正在使用do while循环,并且,当设置时到了10,我不断得到一个似乎是无限循环的东西,因为它只是不断重复而不是按预期结束。我没有编译器错误。这是一项任务。任何帮助表示赞赏。这是我写的代码:

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

namespace hangman_projectW4
{
    class Program
    {
        static void Main(string[] args)
        {
            //variable list
            char letter0 = 'k';
            char letter1 = 'i';
            char letter2 = 'd';
            char letter3 = 'd';
            char letter4 = 'e';
            char letter5 = 'r';
            char letter;
            int score = 0;
            string user = "";

            //inputs inputs
            Console.ForegroundColor = ConsoleColor.Green;
            user = Console.ReadLine();
            Console.WriteLine("Hi! Welcome to Hangman.");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Player, please enter a letter at prompt.");
            Console.Write("_________________________________________________________________");
            user = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Green;

            do
            {
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");                

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");               

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }

            user = Console.ReadLine();
            Console.WriteLine("Please enter a letter: ");
            letter = char.Parse(Console.ReadLine());

            user = Console.ReadLine();
            if (letter == letter0 || letter == letter1 || letter == letter2 || letter == letter3 || letter == letter4 || letter == letter5)
            {
                Console.WriteLine("You guessed correctly!");

            }
            else
            {
                Console.WriteLine("Incorrect. Try Again.");
                score++;
            }
            }while (score <= 10);

            //outputs
            user = Console.ReadLine();
            user = Console.ReadLine();
            Console.Write("_________________________________________________________________");
            user = Console.ReadLine();
            Console.Write("Guessed Incorrectly: " + score);
            Console.ReadLine();
            Console.ReadLine();
            Console.Write("Thank you for playing Hangman.");
            Console.ReadLine();
        }
    }
}

另外,如果有更好的方法来编写代码,那么我就不会重复if else十次......指针会很棒!感谢。

1 个答案:

答案 0 :(得分:0)

如果你努力尝试它会结束。一旦您的分数达到11分或更高,您的代码可能会继续并继续要求再次尝试。停止复制代码。更聪明,最小化您的代码!你的循环反复重复同样的事情,甚至没有检查你是否已经达到10分以上(score)。

将你的循环缩短为:

do
{
    Console.WriteLine("Please enter a letter: ");
    letter = char.Parse(Console.ReadLine());

    if (letter == letter0 ||
        letter == letter1 ||
        letter == letter2 ||
        letter == letter3 ||
        letter == letter4 ||
        letter == letter5)
    {
        Console.WriteLine("You guessed correctly!");
    }
    else
    {
        Console.WriteLine("Incorrect. Try Again.");
        score++;
    }
} while (score <= 10);

下一步是将所有字母添加到集合或列表中,并缩短if语句。 ; - )