错误 2 使用未分配的局部变量'Y'
我再次遇到错误,我无意打扰任何人,但我在这段代码上遇到错误:
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.....");
}
}
}
}
感谢您的回答!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的变量
char Y
在使用前未初始化。声明时尽量给出默认值。编辑 看来您希望用户输入一些内容,并将其分配给变量 Y。尝试:
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
是一个局部变量,未分配任何内容。因此,您可以在if
语句之前为其分配任何值以进行比较。Y
is a local variable which isn't assigned anything. So, you assign it any value before theif
statement to make any comparison.您没有将 Y 设置为任何内容,也没有从键盘上读取任何内容。
You're not setting Y to anything, and you're also not reading anything from the keyboard.
您可以显式地将其设置为 null。
这将消除编译器错误。
编译器错误的根本原因是,当它去编译if条件时,没有任何东西被分配给Y。上面被认为是一个赋值。
You could explicitly set it to null.
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.