c# - 错误 2 使用未分配的局部变量 'Y'

标签 c# .net variables local-variables unassigned-variable

我又遇到了一个错误,我不想打扰任何人,但我在这段代码上遇到了错误:

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

namespace Input_Program
{
    class Program
    {
       private static void Main()
        {

           char Y;
            char N;

           Console.WriteLine("Welcome to my bool program!");
           Console.WriteLine("Input a NON capital y or n when told to.");




            if(Y == 'y')
            {
                Console.WriteLine("Thank you,Please wait.....");
            }
        }
    }
}

感谢您的回答!

最佳答案

您的变量 char Y 在使用前未初始化。声明的时候尽量给一个默认值。

编辑 看起来你想让用户输入一些东西,然后把它赋值给变量 Y。试试:

Y = Console.ReadKey().KeyChar;

关于c# - 错误 2 使用未分配的局部变量 'Y',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5400485/

相关文章:

c# - 如何从数据 : URIs? 到 "download"

c# - .NET Framework 和兼容性

c# - IValueConverter 和可见性

c# - ExceptionAggregator.cs 在哪里

c - C 中虚拟内存的组织

C# 如何以编程方式更改播放设备

c# - 在 C# 中搜索嵌套列表 <> 的最快方法

bash - 为什么变量赋值用空格替换制表符

python - Python 中的迭代器(循环变量)

c# - Nhibernate 映射的内部类和 InternalsVisibleTo