c# - 你如何检索变量?

标签 c#

<分区>

这是我遇到问题的代码,我正在尝试检索错误猜测次数 ( numbWrong ) 的值。怎么查不到猜错的次数?

...
{
    Console.WriteLine("Enter your guess letter using lowercase only.");
    char input = Console.ReadLine().ToCharArray()[0];

    for (int i = 0; i < myWord.Length; i++)
    {
        //if the user guessed right, replace the correct dash and display to the user
        if (myWord[i] == input)
        {
            count++; //update the count
            a[i] = input;  //if guess is correct, dash is replaced by what the user used as input

            //show the new dash array mixed with correct guesses
            for (int j = 0; j < a.Length; j++)
            {
                Console.Write(a[j] + " ");
            }
            else
            {
                numbWrong += 1;
            }
        }

        Console.WriteLine();
        Console.WriteLine("You guessed it right after ");
        Console.Write(numbWrong);
        Console.Write(" incorrect guesses.");
    }

错误:

"Use of unassigned local variable 'numbWrong' "

最佳答案

您的变量未初始化。将其从循环中取出并将其放入您的代码中,如下所示。希望有所帮助。编辑:prescot 先得到它。但是答案相同。

int numbWrong = 0; //change
for (int i = 0; i < myWord.Length; i++)
{
    //if the user guessed right, replace the correct dash and display to the user
    if (myWord[i] == input)
    {
        count++; //update the count
        a[i] = input;  //if guess is correct, dash is replaced by what the user used as input

        //show the new dash array mixed with correct guesses
        for (int j = 0; j < a.Length; j++)
        {
            Console.Write(a[j] + " ");
        }
    }
    else
    {
        numbWrong += 1;
    }
}

关于c# - 你如何检索变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35111793/

相关文章:

c# - 如果符号尚未编码,如何对其进行编码?

c# - 如何将端口接收到的字节转换回字符串?

c# - 在不使用 EF7 添加不变性的情况下限制属性的唯一性

c# - ASP.NET Core 表单 POST 导致 HTTP 415 Unsupported Media Type 响应

c# - 修改字典值是可能的。什么是正确的方法?

c# - JavaScript 日历问题

c# - 如何在 ASP.NET MVC 中实现 "natural"url 方案路由表

c# - 在 C# 中将 float 保存到二进制文件并在 C 中打开

c# - ASP.NET 移动应用程序通过请求简单的 GET 操作获取 "500 Internal Sever Error"

c# - HttpContext.Current.Request.Url.Host 它返回什么?