开发者

Error 2 Use of unassigned local variable 'Y'

开发者 https://www.devze.com 2023-02-18 22:18 出处:网络
Again i run into an error i don\'t mean to bug anyone but I\'m getting an error on this code: using System;

Again i run into an error i don't mean to bug anyone but I'm getting an error on this code:

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.....");
            }
        }
    }
}

Thanks for you answers!


Your variable char Y is not initialized before using. Try to give a default value when declaring.

EDIT It seems that you want the users to input something, and assign it to the variable Y. Try:

Y = Console.ReadKey().KeyChar;


if(Y == 'y')

Y is a local variable which isn't assigned anything. So, you assign it any value before the if statement to make any comparison.

Y = 'a';  // or some character 


You're not setting Y to anything, and you're also not reading anything from the keyboard.


You could explicitly set it to null.

char Y = '<whatever_is_the_default_char>';

That would get rid of the compiler error.

The root cause of the compiler error is that when it goes to compile the if conditional nothing as been assigned to Y. The above is considered an assignment.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号